Solved

Check if a process is running over X min in vbs

Posted on 2013-05-20
9
1,064 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
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!

 

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 500 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

Technology Partners: 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

Suggested Solutions

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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