
我想找出一種方法,使用命令提示字元在 60 秒內關閉程式。類似於電腦定時關機(shutdown /s /t )和taskill。
有任何想法嗎?這可能嗎?
答案1
我如何才能在 60 秒內關閉程式?
您可以使用ping
或timeout
來產生時間延遲。
timeout
在 Windows XP 中不可用。timeout
給你一個倒數計時。
ping
解決方案
若要在 60 秒後終止,notepad
請使用下列命令:
ping 127.0.0.1 -n 61 > nul && taskkill /im notepad.exe
筆記:
ping
您需要 61 次 ping,因為s之間有 1 秒的延遲。
例子:
F:\test>time /t && ping 127.0.0.1 -n 61 > nul && taskkill /im notepad.exe && time /t
17:56
SUCCESS: Sent termination signal to the process "notepad.exe" with PID 8084.
17:57
帶有環回位址的 PING 指令也會產生延遲,每個連續 ping 之間有 1 秒的延遲。在測試中,PING 消耗的處理器時間比 Sleep.exe 或 Timeout.exe 少,這允許其他進程在背景執行。 PING 指令只能用 Ctrl-C 中斷。資料來源:Clay Calvert(usenet 2001。)
例如延遲 40 秒:
PING -n 41 127.0.0.1>nul
來源暫停
timeout
解決方案
若要在 60 秒後終止,notepad
請使用下列命令:
timeout /t 60 && taskkill /im notepad.exe
筆記:
timeout
在 Windows XP 上無法使用。timeout
執行不力。如果您執行“超時 1”,它將等到“下一秒”,這可能會在 0.1 秒內發生。嘗試執行“超時 1”幾次並觀察延遲的差異。對於 5 秒或更長時間,可能沒什麼大不了的,但是對於 1 秒的延遲,效果就很差了。
例子:
F:\test>time /t && timeout /t 60 && taskkill /im notepad.exe && time /t
18:07
Waiting for 0 seconds, press a key to continue ...
SUCCESS: Sent termination signal to the process "notepad.exe" with PID 5412.
18:08
進一步閱讀
- Windows CMD 命令列的 AZ 索引- 與 Windows cmd 行相關的所有內容的絕佳參考。
- 暫停- 延遲執行幾秒鐘或幾分鐘,以便在批次檔中使用。
- 平- 測試網路連線 - 如果成功,ping 將傳回 IP 位址。
- 如何在Windows命令提示字元下休眠5秒? (或DOS)
答案2
嘗試使用任務調度程序。
- 配置事件日誌記錄,以便在啟動所需程式時引發事件。
- 配置計劃任務以在記錄 #1 中的特定事件時執行。在排程任務中,可以設定延遲(在您的情況下為 60 秒),並且可以啟動任何命令/腳本。
答案3
試試我的例子:
choice /t 5 /d a /c ab /n >nul & taskkill /im URprocess.exe
解釋:“choice”等待5秒並退出,然後taskkill關閉“URprocess”
答案4
我使用 Windows 10 提供的 scriptrunner.exe。您可以在以下位置找到文件:https://ss64.com/nt/scriptrunner.html
我經常將一堆電子書轉換為 epub,但轉換經常半途而廢。所以我做了一個包含以下內容的批次。
@echo off
for /F "delims=" %%a in (files.txt) do (
if not exist "%%a.epub" ScriptRunner.exe -appvscript "c:\Program Files (x86)\calibre2\ebook-convert.exe" "%%a" "%%a.epub" -appvscriptrunnerparameters -timeout=60
if exist "%%a.epub" del "%%a"
)
Files.txt包含所有需要轉換路徑的檔案。我使用 calibre 中的 ebook-convert.exe 進行轉換。參數 -timeout=60 是以秒為單位的時間(此處為 60),之後執行停止,如果轉換提前準備好,則執行下一行。
這一切都運作良好。