How might I go about limiting the execution time of a command line program in Windows? I know in Linux there is timeout
/timelimit
available, but I have been unable to find similar utilities for Windows.
-
1It's probably best to write a Powershell or Python script to do this.– BreakthroughDec 17, 2012 at 17:22
5 Answers
start yourprogram.exe
ping 127.0.0.1 -n 10
taskkill /im yourprogram.exe
If you put this in notepad and save it as .bat
it will create a batch file that will do this.
I have used 10 seconds as an example, just tweak to your needs.
Also, if you are using Vista or above you can scrap the ping
line and use the much easier timeout
command in it's place
timeout /t 10
-
1How would this handle arguments and multiple instances of yourprogram? It would be nice if start would return a PID, but no such luck...– ayuDec 17, 2012 at 16:15
-
You can pass arguments with
start yourprogram.exe yourarguments
. Yeah it would,taskkill
has an option to kill by PID but you would have to extract it fromtasklist
first based on name, which is back at square one if you have multiple instances. This is just a limitation of cmd.– Bali CDec 17, 2012 at 16:18 -
1
-
As I know there seems no such a function exist, but I think it's doable if you make it run as a windows task via Task Scheduler, from there you can set a limitation of execution, the setting named as: Stop the task if it runs longer than:, you can get more detail from here: http://technet.microsoft.com/en-us/library/cc722178.aspx
If you can be bothered with using PowerShell:
$app = Start-Process -PassThru -FilePath 'notepad' -ArgumentList 'test.txt'
will start notepad with an argument of test.txt (you can separate multiple arguments by ,
.
Start-Sleep -Seconds 10
will sleep for 10s.
Either $app.Kill()
or taskkill /PID $app.Id
will kill the started application.
If you prefer a cmd solution that will only call PowerShell to start the process and then store it's PID in a variable:
for /F %A in ('powershell -Command "(Start-Process -PassThru -FilePath 'notepad' -ArgumentList 'test.txt').Id"') do set PID=%A
(when called from a .bat/.cmd you have to use %%A
instead of %A
). You can then use @BaliC's methods of timing out and taskkill /PID %PID%
to kill the started application.
-
this still waits for the full 10 seconds.
Wait-Job
orWait-Process
will be much better because it'll return immediately if the command exits early– phuclvJun 29, 2023 at 1:47
Solution :
In windows it needs to make three batch files
process.bat
@ECHO OFF
start /b cm.bat
start cmd.exe /c run.bat
EXIT
cm.bat
@ECHO OFF
a.exe < input.txt > out.txt
taskkill /im a.exe /f
taskkill /im cmd.exe /f
EXIT
run.bat
@ECHO off
timeout /t 5
taskkill /im a.exe /f
taskkill /im cmd.exe /f
EXIT
Now Run process.bat . it will start both cm.bat and run.bat simultaneously/parallely. When a.exe from cm.bat terminates; cm.bat kills run.bat and thus it will work as ubuntu timeout.
-
1The best answer. However it would be even better to use custom window title:
start "a.exe killer" cmd.exe /c run.bat
together withtaskkill /fi "WindowTitle eq a.exe killer" /f
and so on– SmylicFeb 21, 2021 at 19:33
It's much easier in PowerShell than cmd because there's a Wait-Job
cmdlet for this. You can run your app like this
$timeout = 60
$job = sajb { yourprogram.exe }; if (-not ($job | wjb -T $timeout)) { spjb $job }
But it's easier to put it into a function in the profile for reuse by running notepad $PROFILE
in PowerShell and paste this snippet
Function Invoke-WithTimeOut {
Param (
[int]$timeout,
[scriptblock]$script
)
$job = Start-Job $script
$done = $job | Wait-Job -TimeOut $timeout
if (-not $done) { Stop-Job $job }
}
Then you can use it in any PowerShell sessions. For example to run yourprogram.exe
with 40s timeout
Invoke-WithTimeOut 40 { yourprogram.exe }
This works even when there are multiple instances of the same app running, unlike the other taskkill
solutions. Besides it'll return immediately if the command exits before time out so you don't have to wait the full time
You can replace Start-Job
with Start-ThreadJob
for better performance but in normal cases they don't make a difference
If cmd is really needed then you can call this from a batch file
powershell -NoProfile -ExecutionPolicy Bypass -C "Invoke-WithTimeOut 40 { yourprogram.exe }"
From Windows 10 (1607+) there's also a native solution
ScriptRunner.exe -appvscript yourprogram "with" "args" -appvscriptrunnerparameters -wait -timeout=60
If you change Start-Job
/Start-ThreadJob
and Wait-Job
to Start-Process
and Wait-Process
then the result will be the same as the ScriptRunner solution above