Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

process kill script

53 views
Skip to first unread message

Alexis Zorba

unread,
Feb 4, 2008, 9:07:30 AM2/4/08
to
Hello all,

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


Pegasus (MVP)

unread,
Feb 4, 2008, 9:32:22 AM2/4/08
to
"Alexis Zorba" <ko...@nospam.org.cn> wrote in message
news:mn.23c77d828...@nospam.org.cn...

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


Alexis Zorba

unread,
Feb 4, 2008, 1:19:01 PM2/4/08
to
Pegasus (MVP) used his keyboard to write :

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)


Pegasus (MVP)

unread,
Feb 4, 2008, 1:40:38 PM2/4/08
to

"Alexis Zorba" <ko...@nospam.org.cn> wrote in message
news:mn.24c37d82e...@nospam.org.cn...

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.


Alexis Zorba

unread,
Feb 4, 2008, 1:57:48 PM2/4/08
to
Pegasus (MVP) explained :

-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


Tom Lavedas

unread,
Feb 4, 2008, 2:18:53 PM2/4/08
to

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/

Pegasus (MVP)

unread,
Feb 4, 2008, 2:15:25 PM2/4/08
to

"Alexis Zorba" <ko...@nospam.org.cn> wrote in message
news:mn.24e97d82e...@nospam.org.cn...

. . . and your version of the script?


Alexis Zorba

unread,
Feb 4, 2008, 2:22:24 PM2/4/08
to
Pegasus (MVP) presented the following explanation :

I really dont know what you mean by "script version", you wrote it,
maybe version 0.1? :) sorry, but i couldnt understand.


Pegasus (MVP)

unread,
Feb 4, 2008, 2:28:36 PM2/4/08
to

"Alexis Zorba" <ko...@nospam.org.cn> wrote in message
news:mn.25027d825...@nospam.org.cn...

You modified my script by making certain changes.
I would like to see exactly what these changes are.


Pegasus (MVP)

unread,
Feb 4, 2008, 2:27:38 PM2/4/08
to

"Tom Lavedas" <tglb...@cox.net> wrote in message
news:4ae89fb6-dc06-4a98...@q21g2000hsa.googlegroups.com...

Have another look at my script, please. The single quotes were
there all along!


Alexis Zorba

unread,
Feb 4, 2008, 2:28:25 PM2/4/08
to
Tom Lavedas expressed precisely :

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


Alexis Zorba

unread,
Feb 4, 2008, 2:32:41 PM2/4/08
to
Pegasus (MVP) laid this down on his screen :

below is the contents of kp.vbs:

MaxRuntime = 15 * 60 * 1000 '15 minutes
WaitTime = 10000 '10 seconds

app="opera.exe"

Tom Lavedas

unread,
Feb 4, 2008, 2:53:48 PM2/4/08
to
On Feb 4, 2:27 pm, "Pegasus \(MVP\)" <I....@fly.com.oz> wrote:
> "Tom Lavedas" <tglba...@cox.net> wrote in message

>
> news:4ae89fb6-dc06-4a98...@q21g2000hsa.googlegroups.com...
>
>
>
> > On Feb 4, 1:19 pm, Alexis Zorba <ko...@nospam.org.cn> wrote:
> >> Pegasus (MVP) used his keyboard to write :
>
> >> > "Alexis Zorba" <ko...@nospam.org.cn> wrote in message
> >> >news:mn.23c77d828...@nospam.org.cn...
> >> >> Hello all,
>
> >> >> 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'"
{snip}

> > 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/
>
> Have another look at my script, please. The single quotes were
> there all along!

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.

Pegasus (MVP)

unread,
Feb 4, 2008, 2:53:46 PM2/4/08
to
< snip >

> below is the contents of kp.vbs:
>
> MaxRuntime = 15 * 60 * 1000 '15 minutes
> WaitTime = 10000 '10 seconds
> app="opera.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
>
>

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.


Tom Lavedas

unread,
Feb 4, 2008, 2:57:27 PM2/4/08
to

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)

Pegasus (MVP)

unread,
Feb 4, 2008, 3:01:32 PM2/4/08
to
< snip >

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

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.


Alexis Zorba

unread,
Feb 5, 2008, 7:03:55 AM2/5/08
to
Pegasus (MVP) wrote :

Thank you very much Pegasus, it works now!


Pegasus (MVP)

unread,
Feb 5, 2008, 7:59:33 AM2/5/08
to

"Alexis Zorba" <ko...@nospam.org.cn> wrote in message
news:mn.2b4b7d825...@nospam.org.cn...

Thanks for the feedback. I suppose you now realise why
I needed to see ***your*** version of the script.


Alexis Zorba

unread,
Feb 7, 2008, 6:10:38 AM2/7/08
to
Pegasus (MVP) formulated on Salı :

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


Pegasus (MVP)

unread,
Feb 7, 2008, 7:01:55 AM2/7/08
to

"Alexis Zorba" <ko...@nospam.org.cn> wrote in message
news:mn.3b167d827...@nospam.org.cn...
> Pegasus (MVP) formulated on Salý :

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.


Tom Lavedas

unread,
Feb 7, 2008, 9:13:56 AM2/7/08
to

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.

0 new messages