Script to kill process after a set amount of time.

I have a process (*.exe) that runs on a server on regular intervals. I would like to create a script that would be run by Windows Scheduler to automatically kills the process if it runs for more then 5min.

The script can be a .bat or .vbs file.
PROACTIVETGAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rob MinersCommented:
Test this with notepad before implementing, you will have to modify notepad to reflect your .exe

strComputer = "."
Set oWmi = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcessList = oWmi.ExecQuery _
("Select * from Win32_Process Where Name = 'notepad.exe'")

If colProcessList.Count > 0 Then

' sleep for 5 minutes (in milliseconds)
WScript.Sleep 5 * 60 * 1000

' check if notepad.exe is still in memory
Set colProcessList = oWmi.ExecQuery _
("Select * from Win32_Process Where Name = 'notepad.exe'")

' if notepad is in memory, it will be terminated
For Each oProcess in colProcessList
oProcess.Terminate()
Next
End If

WScript.quit

' ref# http://www.pcreview.co.uk/forums/kill-process-if-running-longer-than-15min-t1497183.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobSampsonCommented:
If the task you want to kill is also run by Task Scheduler, you could create a scheduled to run 5 or 10 minutes after that, which just runs
taskkill /F /IM process.exe

I think that would be enough.

Regards,

Rob.
0
SommerblinkCommented:
Well, I'll just inject a newly minted piece of wisdom...

Do not... I repeat... DO NOT attempt to modify the raw XML file which represents the scheduled task in the System32\Tasks folder. :)

I just found out that the files are hashed (quite possibly with some timestamps too) and there are (at least) four registry keys that need to be deleted and the original task re-imported to resolve the 'corrupted' scheduled task.

And even after all that trouble, manually editing the XML file and telling it to cancel after 5 minutes (300 seconds), it still didn't seem to work properly. But I could be mistaken.

Nevertheless, the point to take home today is that editing the scheduled task files by hand is a terrible idea and I will never, ever, EVER think about trying that again. :)
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

PROACTIVETGAuthor Commented:
This did it
0
Rob MinersCommented:
Your welcome :)
0
RobSampsonCommented:
@Sommerblink, I actually found a situation just the other day where I *had* to modify the XML of a scheduled task, due to a bug with the interactivity of a task.

It's a lenghty thread, but see here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28106233.html#a39425644

You will see that if you search for
remove <LogonType>InteractiveToken</LogonType>

you will find many people have had to do it.  I haven't had any experience with a task going belly up, and I hope I never do.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.