- 1 year ago
Cmd batch: Run an exe but kill if after a period of time, if doesn't resolve
You can achieve this in a batch script by using the start
command along with the timeout
command. The start
command will run the executable in a new window, and the timeout
command will wait for a specified amount of time and then terminate the process if it doesn't complete.
Here's an example of how you can do it:
batch@echo off
set "exePath=C:\path\to\your\executable.exe"
set "timeoutSeconds=60"
start "" "%exePath%"
timeout /t %timeoutSeconds% /nobreak
taskkill /im "executable.exe" /f
Replace C:\path\to\your\executable.exe
with the actual path to your executable, and 60
with the number of seconds you want to wait before killing the process.
The start
command opens the executable in a new window, and the script waits for the specified number of seconds using the timeout
command. If the process completes within the given time, it will continue to run. If not, the taskkill
command is used to forcibly terminate the process.
Note that the /f
option with taskkill
forcefully terminates the process. Be cautious while using it, as it may lead to data loss or unexpected behavior if the process is not responding.