CJSmith dot me

I dump stuff I find useful here

Within a batch file run a program for a certain length of time before killing it

Published / by Chris Smith / Leave a Comment

I needed to run a scheduled batch job and kill it off after a certain amount of time, this was because the program would run what it needed and then run as normal (If it exists then there is no reason to use start or timeout). As it actually ran as a service I didn’t want to leave it running in this way.
As I use Taskkill on the programs exe name, obviously don’t use it if it will be run on a server where it is running other executables with the same program name.
I replaced timeout with ping as the process launched fully utilises the CPU cores.

ECHO Running the program with start allows the batch script to move on to the next line
start "DBCapture" "d:\myprogram.exe" -runparameter1 -runparameter2
ECHO Waiting 120 seconds before killing off the task
ECHO Credit For lower CPU usage, instead of using WAIT use Ping as suggested on http://ss64.com/nt/timeout.html
PING -n 121 127.0.0.1>nul
ECHO Kill the program
Taskkill /IM "myprogram.exe" /F

Credits: I replaced my use of timeout based on a suggestion on SS64.com

Leave a Reply