Solved

Check if a process is running over X min in vbs

Posted on 2013-05-20
9
992 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

18 Experts available now in Live!

Get 1:1 Help Now