I want to figure out a way to use command prompt to close a program in say... 60 seconds. Similiar to the computer timed shutdown (shutdown /s /t ) and the taskill.
Any ideas? Is this possible?
04 Answers
How can I close a program in say... 60 seconds?
You can use ping or timeout to produce a time delay.
timeoutis not available in Windows XP.timeoutgives you a countdown.
ping solution
To kill notepad after 60 seconds use the following command:
ping 127.0.0.1 -n 61 > nul && taskkill /im notepad.exeNote:
- You need 61 pings as there is a delay of 1s between
pings.
Example:
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:57A delay can also be produced by the PING command with a loopback address, there is a delay of 1 second between each consecutive ping. In tests PING consumes less processor time than Sleep.exe or Timeout.exe, this allows other processes to run in the background. The PING command can only be interrupted with Ctrl-C. Source: Clay Calvert (usenet 2001.)
e.g. for a delay of 40 seconds:
PING -n 41 127.0.0.1>nul
Source timeout
timeout solution
To kill notepad after 60 seconds use the following command:
timeout /t 60 && taskkill /im notepad.exeNotes:
timeoutis not available on Windows XP.timeoutis poorly implemented. If you do a "timeout 1", it will wait until the "next second," which could occur in .1 seconds. Try doing "timeout 1" a few times and observe the difference in delay. For 5 seconds or more, it may not be a big deal, but for a 1 second delay it works poorly.
Example:
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:08Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- timeout - Delay execution for a few seconds or minutes, for use within a batch file.
- ping - Test a network connection - if successful, ping returns the ip address.
- How to sleep for 5 seconds in Windows's Command Prompt? (or DOS)
Try using Task Scheduler.
- Configure Event Logging so an event is raised when the desired program is launched.
- Configure a Scheduled Task to run when the particular event from #1 is logged. In the scheduled task, a delay can be set (60 seconds in your case) and any commands/scripts can be launched.
Try my example:
choice /t 5 /d a /c ab /n >nul & taskkill /im URprocess.exe
Explanation: "choice" waits 5 seconds and exits, then taskkill closes "URprocess"
I use scriptrunner.exe supplied with windows 10. I had to search for mine, wasn't in the path. You can find documentation at
I often convert a bunch of ebooks to epub but the conversion often stuck halfway. So I made a batch with the following content.
@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 contains all the files with paths that need to be converted. I use ebook-convert.exe from calibre to do the conversion. The argument -timeout=60 is the time in seconds (here 60) after which the execution stops, if the conversion is ready earlier the next line is executed.
All this works well.