From Cmd Prompt - How to Time a Program to be exited?

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?

0

4 Answers

How can I close a program in say... 60 seconds?

You can use ping or timeout to produce a time delay.

  • timeout is not available in Windows XP.
  • timeout gives 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.exe

Note:

  • 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:57

A 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.exe

Notes:

  • timeout is not available on Windows XP.
  • timeout is 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:08

Further Reading

Try using Task Scheduler.

  1. Configure Event Logging so an event is raised when the desired program is launched.
  2. 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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like