Windows – Stop and start batch file every hour

batchwindows

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.

Best Answer

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.