Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Determine when a process starts and stops

Reply
 
Old 08-13-2009   #1 (permalink)
OldDog


 
 

Determine when a process starts and stops

Hi,

I would like to write a script that waits for a process to start,
starts counting and then record when it stops.

So far I have this. It works if I start it after I start the process.
I need it to lurk until the process starts:

Function isRunning(process)
WriteToLog "isRunning", "Looking for process=" & process
Set colProcessList = oWMIService.ExecQuery("Select * from
Win32_Process Where Name = """ & process & """" )
isRunning = colProcessList.Count
End Function

Sub WriteToLog(strProg, strData)
strCurrentTime = Now()
oLogFile.WriteLine(Now() & " | " & strProg & " | " & strData)
End Sub

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKLM = &H80000002

Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!
\root\cimv2")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFSO.OpenTextFile("c:\temp\ocsetup.txt", ForWriting,
True)

If isRunning("ocsetup.exe") Then
C = 666
Do Until C = 0
C = isRunning("ocsetup.exe")
WScript.Echo "."
WriteToLog "main","Runing ocsetup.exe"
WScript.Sleep 1000
Loop
WriteToLog "main","Finished Runing ocsetup.exe"
End If

My System SpecsSystem Spec
Old 08-13-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Determine when a process starts and stops


"OldDog" <mikef2691@xxxxxx> wrote in message
news:3df93d1b-679d-4e0b-9f62-e2b80cfa1f40@xxxxxx
Quote:

> Hi,
>
> I would like to write a script that waits for a process to start,
> starts counting and then record when it stops.
>
> So far I have this. It works if I start it after I start the process.
> I need it to lurk until the process starts:
>
> Function isRunning(process)
> WriteToLog "isRunning", "Looking for process=" & process
> Set colProcessList = oWMIService.ExecQuery("Select * from
> Win32_Process Where Name = """ & process & """" )
> isRunning = colProcessList.Count
> End Function
>
> Sub WriteToLog(strProg, strData)
> strCurrentTime = Now()
> oLogFile.WriteLine(Now() & " | " & strProg & " | " & strData)
> End Sub
>
> Const wbemFlagReturnImmediately = &h10
> Const wbemFlagForwardOnly = &h20
> Const ForReading = 1
> Const ForWriting = 2
> Const ForAppending = 8
> Const HKEY_LOCAL_MACHINE = &H80000002
> Const HKLM = &H80000002
>
> Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!
> \root\cimv2")
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oLogFile = oFSO.OpenTextFile("c:\temp\ocsetup.txt", ForWriting,
> True)
>
> If isRunning("ocsetup.exe") Then
> C = 666
> Do Until C = 0
> C = isRunning("ocsetup.exe")
> WScript.Echo "."
> WriteToLog "main","Runing ocsetup.exe"
> WScript.Sleep 1000
> Loop
> WriteToLog "main","Finished Runing ocsetup.exe"
> End If
While you could achieve your aim by using your method, I suspect that your
WMI query will cause a spike in processor loading each time it runs. To
avoid this spike you could use the code below. It is based on an idea by the
Microsoft Scripting Guy.

iPoll = 5 'Time in seconds between polls
sProcess = "notepad" 'Name of application to be monitored

Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set oWshShell = CreateObject("WScript.Shell")

Set cProcesses = oWMIService. _
ExecNotificationQuery("select * from __instanceCreationEvent " _
& " within " & iPoll & " where TargetInstance isa 'Win32_Process'")

Do
Set oProcess = cProcesses.NextEvent
If InStr(oProcess.TargetInstance.CommandLine, sProcess) > 0 _
Then WScript.Echo Date, Time, "Process " & sProcess & ".exe was started."
Loop

You would need to run two VB Scripts at the same time: One to monitor the
launch of your program, the other for shutting it down. Just replace the
string "Creation" with "Deletion".


My System SpecsSystem Spec
Old 08-15-2009   #3 (permalink)
OldDog


 
 

Re: Determine when a process starts and stops

On Aug 13, 4:22*pm, OldDog <mikef2...@xxxxxx> wrote:
Quote:

> Hi,
>
> *I would like to write a script that waits for a process to start,
> starts counting and then record when it stops.
>
> So far I have this. It works if I start it after I start the process.
> I need it to lurk until the process starts:
>
> Function isRunning(process)
> WriteToLog "isRunning", "Looking for process=" & process
> Set colProcessList = oWMIService.ExecQuery("Select * from
> Win32_Process Where Name = """ & process & """" )
> isRunning = colProcessList.Count
> End Function
>
> Sub WriteToLog(strProg, strData)
> * * * * strCurrentTime = Now()
> * * * * oLogFile.WriteLine(Now() & " | " & strProg & " | " & strData)
> End Sub
>
> Const wbemFlagReturnImmediately = &h10
> Const wbemFlagForwardOnly = &h20
> Const ForReading = 1
> Const ForWriting = 2
> Const ForAppending = 8
> Const HKEY_LOCAL_MACHINE = &H80000002
> Const HKLM = &H80000002
>
> Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!
> \root\cimv2")
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oLogFile = oFSO.OpenTextFile("c:\temp\ocsetup.txt", ForWriting,
> True)
>
> If isRunning("ocsetup.exe") Then
> * * * * C = 666
> * * * * Do Until C = 0
> * * * * * * * * C = isRunning("ocsetup.exe")
> * * * * * * * * WScript.Echo "."
> * * * * * * * * WriteToLog "main","Runing ocsetup.exe"
> * * * * * * * * WScript.Sleep 1000
> * * * * Loop
> * * * * WriteToLog "main","Finished Runing ocsetup.exe"
> End If
I found a way to get it to lurk:
I made the main part of the script a Sub called TimeIt and then got it
to call itself until the processes started.

Sub TimeIt
Quote:

> If isRunning("ocsetup.exe") Then
> C = 666
> Do Until C = 0
> C = isRunning("ocsetup.exe")
> WScript.Echo "."
> WriteToLog "main","Runing ocsetup.exe"
> WScript.Sleep 1000
> Loop
> WriteToLog "main","Finished Runing ocsetup.exe"
Else
WScript.Sleep 1000 ' Wait a second and try again
TimeIt
Quote:

> End If
End Sub
My System SpecsSystem Spec
Old 08-15-2009   #4 (permalink)
Al Dunbar


 
 

Re: Determine when a process starts and stops


"OldDog" <mikef2691@xxxxxx> wrote in message
news:30565ffc-5b0c-4ca1-be74-56c910fa36b0@xxxxxx
Quote:

> On Aug 13, 4:22 pm, OldDog <mikef2...@xxxxxx> wrote:
Quote:

>> Hi,
>>
>> I would like to write a script that waits for a process to start,
>> starts counting and then record when it stops.
>>
>> So far I have this. It works if I start it after I start the process.
>> I need it to lurk until the process starts:
>>
>> Function isRunning(process)
>> WriteToLog "isRunning", "Looking for process=" & process
>> Set colProcessList = oWMIService.ExecQuery("Select * from
>> Win32_Process Where Name = """ & process & """" )
>> isRunning = colProcessList.Count
>> End Function
>>
>> Sub WriteToLog(strProg, strData)
>> strCurrentTime = Now()
>> oLogFile.WriteLine(Now() & " | " & strProg & " | " & strData)
>> End Sub
>>
>> Const wbemFlagReturnImmediately = &h10
>> Const wbemFlagForwardOnly = &h20
>> Const ForReading = 1
>> Const ForWriting = 2
>> Const ForAppending = 8
>> Const HKEY_LOCAL_MACHINE = &H80000002
>> Const HKLM = &H80000002
>>
>> Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!
>> \root\cimv2")
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>> Set oLogFile = oFSO.OpenTextFile("c:\temp\ocsetup.txt", ForWriting,
>> True)
>>
>> If isRunning("ocsetup.exe") Then
>> C = 666
>> Do Until C = 0
>> C = isRunning("ocsetup.exe")
>> WScript.Echo "."
>> WriteToLog "main","Runing ocsetup.exe"
>> WScript.Sleep 1000
>> Loop
>> WriteToLog "main","Finished Runing ocsetup.exe"
>> End If
>
> I found a way to get it to lurk:
> I made the main part of the script a Sub called TimeIt and then got it
> to call itself until the processes started.
>
> Sub TimeIt
Quote:

>> If isRunning("ocsetup.exe") Then
>> C = 666
>> Do Until C = 0
>> C = isRunning("ocsetup.exe")
>> WScript.Echo "."
>> WriteToLog "main","Runing ocsetup.exe"
>> WScript.Sleep 1000
>> Loop
>> WriteToLog "main","Finished Runing ocsetup.exe"
> Else
> WScript.Sleep 1000 ' Wait a second and try again
> TimeIt
Quote:

>> End If
> End Sub
Recursion can be an elegant thing when used to solve certain types of
problems. But this isn't one of them.

Each time a sub calls itself, resources are allocated. Sure they are
released in turn each time the sub exits, but there needs to be enough
resources to allocate.

I just ran the very simple script shown below and got an "out of memory"
error at just over 1700 recursive calls.

count = 0
Call recur
sub recur
count = count + 1
WScript.Echo count
Call recur
End Sub

In general, there needs to be some control over the depth of the recursion.
In this case the control is an external event. If it takes longer than about
28 minutes for the ocsetup process to start running, I'm guessing your
script will error out.

I'd suggest a non-recursive approach such as:

do while not isRunning("ocsetup.exe")
WScript.Sleep 1000 ' Wait a second and try again
loop

do while isRunning("ocsetup.exe")
WScript.Echo "."
WriteToLog "main","Running ocsetup.exe"
WScript.Sleep 1000 ' Wait a second and try again
loop

WriteToLog "main","Finished Runing ocsetup.exe"

This could run for months waiting for ocsetup.exe to start running without
forcing an "out of memory" error.


/Al


My System SpecsSystem Spec
Old 08-16-2009   #5 (permalink)
Pegasus [MVP]


 
 

Re: Determine when a process starts and stops


"urkec" <urkec@xxxxxx> wrote in message
news:17716A99-A42E-46B9-83B4-64A98D29EC8A@xxxxxx
Quote:

> "OldDog" wrote:
>
Quote:

>> On Aug 13, 4:22 pm, OldDog <mikef2...@xxxxxx> wrote:
Quote:

>> > Hi,
>> >
>> > I would like to write a script that waits for a process to start,
>> > starts counting and then record when it stops.
>> >
>> > So far I have this. It works if I start it after I start the process.
>> > I need it to lurk until the process starts:
>> >
>> > Function isRunning(process)
>> > WriteToLog "isRunning", "Looking for process=" & process
>> > Set colProcessList = oWMIService.ExecQuery("Select * from
>> > Win32_Process Where Name = """ & process & """" )
>> > isRunning = colProcessList.Count
>> > End Function
>> >
>> > Sub WriteToLog(strProg, strData)
>> > strCurrentTime = Now()
>> > oLogFile.WriteLine(Now() & " | " & strProg & " | " & strData)
>> > End Sub
>> >
>> > Const wbemFlagReturnImmediately = &h10
>> > Const wbemFlagForwardOnly = &h20
>> > Const ForReading = 1
>> > Const ForWriting = 2
>> > Const ForAppending = 8
>> > Const HKEY_LOCAL_MACHINE = &H80000002
>> > Const HKLM = &H80000002
>> >
>> > Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!
>> > \root\cimv2")
>> > Set oFSO = CreateObject("Scripting.FileSystemObject")
>> > Set oLogFile = oFSO.OpenTextFile("c:\temp\ocsetup.txt", ForWriting,
>> > True)
>> >
>> > If isRunning("ocsetup.exe") Then
>> > C = 666
>> > Do Until C = 0
>> > C = isRunning("ocsetup.exe")
>> > WScript.Echo "."
>> > WriteToLog "main","Runing ocsetup.exe"
>> > WScript.Sleep 1000
>> > Loop
>> > WriteToLog "main","Finished Runing ocsetup.exe"
>> > End If
>>
>> I found a way to get it to lurk:
>> I made the main part of the script a Sub called TimeIt and then got it
>> to call itself until the processes started.
>>
>> Sub TimeIt
Quote:

>> > If isRunning("ocsetup.exe") Then
>> > C = 666
>> > Do Until C = 0
>> > C = isRunning("ocsetup.exe")
>> > WScript.Echo "."
>> > WriteToLog "main","Runing ocsetup.exe"
>> > WScript.Sleep 1000
>> > Loop
>> > WriteToLog "main","Finished Runing ocsetup.exe"
>> Else
>> WScript.Sleep 1000 ' Wait a second and try again
>> TimeIt
Quote:

>> > End If
>> End Sub
>>
>
>
> You could also try this approach: Issue a WMI notification query to
> monitor
> instance creation events for Win32_Process instances with a specified name
> and when you receive the notification use Win32_Process.ProcessId to wait
> for
> the Win32_Process deletion event. Here is a sample:
>
>
>
> strProcessName = "Notepad.exe"
>
> Set objWmi = GetObject("winmgmts:")
>
> Set objCreationSource = objWmi.ExecNotificationQuery _
> ("Select * From __InstanceCreationEvent " _
> & "Within 1 " _
> & "Where TargetInstance Isa 'Win32_Process' " _
> & "And TargetInstance.Name = '" & strProcessName & "'")
>
> Set objCreationEvent = objCreationSource.NextEvent
>
> Set objDeletionSource = objWmi.ExecNotificationQuery _
> ("Select * From __InstanceDeletionEvent " _
> & "Within 1 " _
> & "Where TargetInstance Isa 'Win32_Process' " _
> & "And TargetInstance.ProcessId = " _
> & objCreationEvent.TargetInstance.ProcessId)
>
> Set objDeletionEvent = objDeletionSource.NextEvent
>
> Set objDate = CreateObject("WbemScripting.SWbemDateTime")
> objDate.Value = objCreationEvent.TargetInstance.CreationDate
>
> WScript.Echo "Process: " & strProcessName
> WScript.Echo "Created: " & objDate.GetVarDate
> WScript.Echo "Deleted: " & Now
>
>
> --
> urkec
It seems we're closing the circle - this is pretty much the code I suggested
in my own reply right at the start of this thread.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Vista Starts & stops General Discussion
Diagnostic Policy Service starts then stops Vista networking & sharing
How can I determine what process is running? Vista General
Media Centre stops working just after it starts Media Center
Media Center stops working before it Starts Media Center


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46