Link to home
Start Free TrialLog in
Avatar of johnnyjonathan
johnnyjonathan

asked on

Check if a process is running over X min in vbs

Hello,
I have a process that i need to monitor and check if after it starts it stays for a period of over X min. and if so, then send an email notification

(it's a scheduled task that runs and should close on it's own after about 20 min, and i want to know if it stays on for over 30 min)

Any script to do it?
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, the code here should get you started:
http://blogs.technet.com/b/heyscriptingguy/archive/2005/07/20/how-can-i-determine-the-date-and-time-that-a-process-started.aspx

Just add an email procedure to that, and test against X minutes since .CreationDate and it should be fine.

Alternatively, you can just enter "30 minutes" in the "Stop the task if it runs longer than" box:
User generated image
Regards,

Rob.
Rob's post and mine information below assume you have an OS with Task Scheduler 2.0 (Vista, Windows 7, etc).  The best thing is to add the stop task if running longer than xx minutes as Rob posted.  Now if you really need the email for if the task failed you can create a trigger to happen if he task fails.  

in the task you want to monitor
1.  Create a new trigger
2.  Select on a event
3.  Select Custom
4.  Click New Event Filter
5.  Choose By Source and Task Scheduler
6.  choose the category such as task terminated
click on the xml tag and copy the xml

Now create a new task with a new event trigger.
Paste in the xml on this new task and then add an action item to send an email.

This should now send you an email if the task is terminated.

-Bear
Avatar of johnnyjonathan
johnnyjonathan

ASKER

Hi, the solutions are both good, but i don't want to terminate or check if it's terminated, the request comes from a developer that wants to debug and check why it's taking so long for the process to run (usually should take about 10 min where it takes about 30), and he can't force to shut it down, he just needs an alert, otherwise i'd defiantly use Rob's solution to kill the process.
Rob, thanks so much for the tip!
the site is very us full, i understood it, however, i didn't understand or don't know how to take the time of creation object, and add min to it to preform an action.

objProcess.CreationDate = the time
how do i say

If objProcess.CreationDate + 30 min = do X\Y\Z
OK, so you would use something like
If DateDiff("n", objProcess.CreationDate, Now) > 30 Then
	WScript.Echo "Process is older than 30 minutes."
Else
	WScript.Echo "Process is not olde than 30 minutes."
End If

Open in new window


That uses the DateDiff function:
http://msdn.microsoft.com/en-us/library/xhtyw595(v=vs.84).aspx

to determine the amount of minutes (n) from the CreationDate of the date process, until Now (which will be the current time).  If it's greater than 30 minutes, you will see the first message.

Rob.
Hey Rob, thanks for the tip, something strange though, once i add the script into one portion as -

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

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

For Each objProcess in colProcessList
    
If DateDiff("n", objProcess.CreationDate, Now) > 30 Then
	WScript.Echo "Process is older than 30 minutes."
Else
	WScript.Echo "Process is not olde than 30 minutes."
End If

Next

Open in new window



If the process is active i get the following error:

Microsoft VBScript runtime error (8, 1) : Type mismatch: '[string: "20130527101945.86712"]  - The string being the time stamp

and if the process isn't alive, i get a blank result.
any idea why?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works like a charm! RobSampson, thank you! your answers are not only technically amazing, but great source of knowledge for better understanding in the future.
No problem.  Thanks for the grade, and the kind words.  Glad to help!