Solved

Check if a process is running over X min in vbs

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now