Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Dec 17 '12 at 16:46

This question came from our site for professional and enthusiast programmers.

    
It's probably best to write a Powershell or Python script to do this. –  Breakthrough Dec 17 '12 at 17:22

3 Answers 3

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
share|improve this answer
    
How would this handle arguments and multiple instances of yourprogram? It would be nice if start would return a PID, but no such luck... –  ayu Dec 17 '12 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 from tasklist first based on name, which is back at square one if you have multiple instances. This is just a limitation of cmd. –  Bali C Dec 17 '12 at 16:18
    
+1 Using ping as the timout timer is a funny idea :) –  kol Dec 17 '12 at 18:28

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.