i need a script that will check for a process (example.exe)
if the process is running more than 15minutes, the script will kill it.
need your help
You could run this script. Press Ctrl+C to terminate it.
MaxRuntime = 15 * 60 * 1000 '15 minutes
WaitTime = 10000 '10 seconds
app="'notepad.exe'"
Set objWMIService = GetObject ("winmgmts:\\.\root\cimv2")
do
wscript.echo "Checking " & app
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process where Name=" & app)
For Each objProcess In colProcessList
wscript.sleep MaxRuntime
Set list = GetObject("winmgmts:").execquery _
("Select * from Win32_Process where Name=" & app)
if list.count > 0 then
wscript.echo "Killing " & app
objProcess.Terminate()
end if
Next
wscript.sleep WaitTime
loop until false
First, i want to thank you,
but i have a problem, in my computer opera.exe is running more than 1-2
hours, i changed appname to opera.exe and run script.But it waits in
the screen; checking opera.exe..
when i click OK, it gives an error window writing;
script: c:\kp.vbs
line: 11
char: 1
error: 0x80041017
code: 80041017
source: (null)
You must run the script with cscript.exe, not with wscript.exe.
If you run the command
cscript.exe //h:cscript
from the Command Prompt then cscript.exe will become the
default engine for all .vbs files.
If you still have problems then you must include more
information in your reply:
- Your version of the script
- If the problem happens while opera is active or not.
-im not sure, maybe 5.6?
- the problem happens when opera is running
i did the cscript stuff and got the errors below;
C:\>cscript.exe //h:cscript
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
The default script host is now set to "cscript.exe".
C:>kp.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Checking opera.exe
C:\kp.vbs(11, 1) (null): 0x80041017
There are two problems with the query. Queries are case sensitive and
the name string needs to be enclosed in single quote marks. That is,
this command ...
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process where Name=" & app)
should be ...
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & app & "'")
Also, you wanted to check the the run time. That can be done, like
this ...
app="opera.exe"
nMaxRunTime = 15 ' minutes
Set objWMIService = GetObject ("winmgmts:\\.\root\cimv2")
Do
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & app & "'")
For Each objProcess In colProcessList
nRunTime = datediff("n",
WMIDateStringToDate(objProcess.CreationDate), Now)
if nRuntime > nMaxRunTime then objProcess.Terminate
next
wsh.sleep 30000 ' check every 30 seconds
loop
Function WMIDateStringToDate(dtmInstallDate)
WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
& " " & Mid (dtmInstallDate, 9, 2) & ":" & _
Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate,
_
13, 2))
End Function
This approach can check multiple instances of the same routine and
check existing instances for their current runtime status (not just
from the initiation of the script).
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
. . . and your version of the script?
I really dont know what you mean by "script version", you wrote it,
maybe version 0.1? :) sorry, but i couldnt understand.
You modified my script by making certain changes.
I would like to see exactly what these changes are.
Have another look at my script, please. The single quotes were
there all along!
app="opera.exe"
nMaxRunTime = 15 ' minutes
Set objWMIService = GetObject ("winmgmts:\\.\root\cimv2")
Do
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & app & "'")
For Each objProcess In colProcessList
nRunTime = datediff("n",
WMIDateStringToDate(objProcess.CreationDate), Now)
if nRuntime > nMaxRunTime then objProcess.Terminate
next
wsh.sleep 30000 ' check every 30 seconds
loop
Function WMIDateStringToDate(dtmInstallDate)
WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
& " " & Mid (dtmInstallDate, 9, 2) & ":" & _
Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate,
_
13, 2))
End Function
when i run the script above i get;
kp2.vbs(10, 25) Microsoft VBScript compilation error: Syntax error
below is the contents of kp.vbs:
MaxRuntime = 15 * 60 * 1000 '15 minutes
WaitTime = 10000 '10 seconds
app="opera.exe"
OK, you are correct, I see that now. Also, I couldn't reproduce the
error anymore, though my first c&p failed identically to Mr. Zorba.
However, once I considered the fact that I missed the distinction the
first time, when I removed the quotes from around the name in the
definition line, the error was replicated. Therefore, I must assume
he entered ...
app="opera.exe"
and not
app="'opera.exe'"
as he should have to use your (working) script to address his actual
target application.
Personally, I see how this can happen - even to a 'non-novice' ;o).
Because of that, I think the better place to put the quotes is in the
query itself, not in the name of the app. But, that's just the way I
would do it.
It appears that you fell into the same trap as Tom did.
Consider these lines of code::
app="'notepad.exe'" (this is what I wrote)
app="opera.exe" (this is what you wrote)
In other words, you omitted the essential single quotes!
I will now have a look at the other points that Tom raised.
There was a wordwrap on that line in transit.
nRunTime = datediff("n",
WMIDateStringToDate(objProcess.CreationDate), Now)
Should be on one line ...
nRunTime=datediff("n",WMIDateStringToDate(objProcess.CreationDate),Now)
Or put an underscore charater after the comma ...
nRunTime = datediff("n",_
WMIDateStringToDate(objProcess.CreationDate), Now)
I don't know about your machine but on mine this particular
query is NOT case sensitive. My code will kill notepad.exe
whether app="'notepad.exe'", NOTEPAD.EXE or any
other combination. You may want to test my code in order
to check the two points you raised.
Thank you very much Pegasus, it works now!
Thanks for the feedback. I suppose you now realise why
I needed to see ***your*** version of the script.
Pegasus, i want to ask something,
if multiple instances of the same process is running eg(three
notepad.exe)'s, will your script get the PID's of the each notepad.exe
seperately and count back 15 minutes for each of these? or will it kill
3 of them after the first notepad.exe started to run? i hope i could
tell clearly what i mean..
It will kill the whole lot in one fell swoop. You can easily verify
this, by running three instances of notepad and setting very
short delay times.
Just a note to Alexis to say that the approach I posted does all
matching processes, but only after *each individual item* has run for
the specified timeout period. i.e the requested 15 minutes.
Now you have two choices, depending on your need.