Restart a process after xx minutes

Very new to using PS and hoping I can get a little assistance please.  I haven't had any luck finding an answer in an existing topic.

How can I use the information I get from 

New-TimeSpan -Start (get-process notepad).StartTime 

and make that restart notepad after xx hours or xx minutes have gone by?  Notepad is just an example of course, but I have several processes I need to monitor for uptime and have restarted at specific intervals.

I realize I can make a scheduled task to do the same from a simple .bat file, but I already have a PS script checking these processes' heartbeats.  I'd like to add this bit of functionality within the same script.

Thanks so much for the assist!

September 1st, 2015 2:58am

This may help - The below code will stop and start the process if start time is greater than 4 milliseconds

Get-Process -name notepad | % {
If((New-TimeSpan -Start (get-process -Id $_.Id).StartTime).Milliseconds -ge 4)
{
    Stop-Process $_.Id -force 
}

}
#Start-Process -FilePath C:\Windows\System32\notepad.exe

Changing Milliseconds to Hours and Minutes is possible.

Note: The above is just an example to start - I don't find any reason to stop and start the processes! If you have any business needs then it's well and good.

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 4:29am

Chen that's perfect.  Thank you!

Yeah, it's an odd request, but we have some old Delphi apps with bad memory leaks that need a kick once in awhile to save a call at 3:00AM.  The PS I'm running is to monitor their up state, and now can monitor their uptime and restart as needed.

Thanks again!

September 1st, 2015 5:52am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics