Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if a process is running over X min in vbs

Posted on 2013-05-20
9
Medium Priority
?
1,216 Views
Last Modified: 2013-05-29
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?
0
Comment
Question by:johnnyjonathan
  • 4
  • 4
9 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 39182975
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:
StopTheTaskAfter30Minutes
Regards,

Rob.
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 39183351
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
0
 

Author Comment

by:johnnyjonathan
ID: 39185046
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:johnnyjonathan
ID: 39185056
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39186037
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.
0
 

Author Comment

by:johnnyjonathan
ID: 39201710
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?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 39203119
The CreationDate is a string format, not a date format, so try this code.

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", WMIDateStringToDate(objProcess.CreationDate), Now) > 30 Then
	WScript.Echo "Process is older than 30 minutes."
Else
	WScript.Echo "Process is not older than 30 minutes."
End If

Next

Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm: 
	WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
	Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
	& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function

Open in new window

0
 

Author Closing Comment

by:johnnyjonathan
ID: 39203814
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39204018
No problem.  Thanks for the grade, and the kind words.  Glad to help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question