Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a batch script which starts a program that runs in cmd.exe I have tried using task scheduler to create a task which stops this cmd shell and starts a fresh one every hour to no avail. Are there any other ways to stop and start the same cmd and batch script every hour?

I tried making another batch with the following:

Taskkill /IM cmd.exe

and scheduling that to run every hour but since this batch also runs in a cmd shell it simply kills itself and not the previously running cmd shell.

Any help acheiving the stopping and restarting of the same cmd shell every x intervals would be greatly appreciated.

regards

Shaolin

EDIT:

The reason I want to restart this program every x intervals is because this particular program tends to hang, it may hang after 10 minutes or 10 hours. As a precaution i'd like to have it restart every hour.

share|improve this question

3 Answers 3

It's a little unclear what you're trying to achieve and why, but I think batch scripting is the wrong tool for the job (Especially in 2013).

Powershell will give you far more predictable results and allow you provide far better reliability, for example:

$application = "C:\Windows\System32\notepad.exe"
$sleepTime = 10
$i = 1

while ($i = 1){
    Write-Host "Starting Process"
    $process = Start-Process $application -PassThru

    Write-Host "Waiting " $sleepTime "seconds"
    Start-Sleep -Seconds $sleepTime

    if(!$process.HasExited){
        Write-Host "Killing Process"
        $process.Kill()
    }else{
        echo "Process had already ended"
    }

    Start-Sleep -Seconds 2
}

Although I'm pretty sure there must be a better answer than using an infinite loop.

share|improve this answer
    
+1 For the use of powershell –  Daryl Gill Apr 13 '13 at 18:19

This sounds more like a logic problem. In the batch script you can type exit to close the cmd window within the logic. Otherwise you can look at using stop-process and start-process within powershell. I'd say we need a bit more information 'why' you need a fresh cmd window every hour.

share|improve this answer
    
This particular program tends to hang, it may hang after 10 minutes or 10 hours. As a precaution i'd like to have it restart every hour –  Shaolin Apr 13 '13 at 12:08

You should be able to add a /IF "CPUTIME gt 00:30:00"

taskkill /IM cmd.exe /IF "CPUTIME gt 00:29:00"
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.