Page 1 of 1 1
Topic Options
#163971 - 2006-07-04 06:49 PM Killing Orphaned Processes
Ste Offline
Fresh Scripter

Registered: 2003-05-28
Posts: 10
Hi

I need someone to help me to write a script please.

We have around 60 PCs that run a certain process lets say its called "CONNECT.EXE", the problem is that we seem to now have 2 processes call "CONNECT.EXE" on each machine. This connect program writes records to a SQL database and as such we are getting duplicate records.

I need a script that will read a text file containing machines names, get a PSLIST and then kill the oldest process called CONNECT.EXE

How easy is this? This is causing me serious headaches

Thanks everyone

Top
#163972 - 2006-07-04 06:56 PM Re: Killing Orphaned Processes
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I would strongly recommend that you root cause WHY you have two instances of CONNECT.EXE running in the first place. For example, do you have it in the HKCU and HKLM run key ? Is it in the programs startup folder as well ?

I cant really see how killing processes remotely would fix your issue. Wouldnt the issue come back on next reboot or user login ?

Top
#163973 - 2006-07-04 10:12 PM Re: Killing Orphaned Processes
Ste Offline
Fresh Scripter

Registered: 2003-05-28
Posts: 10
Hi

This is a one time fix for a problem that occured when a script didn't execute properly, a service was shutdown but the orphaned process remained and is still creating duplicate records.

Thanks

Top
#163974 - 2006-07-04 10:32 PM Re: Killing Orphaned Processes
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
If you look in the UDF forum I'm sure you will find some functions to do what you need, specifically Enumprocess() and Readfile().

I think the hard part will be determining which EXE to kill since there will be two.

Top
#163975 - 2006-07-05 09:15 AM Re: Killing Orphaned Processes
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

I think the hard part will be determining which EXE to kill since there will be two.




If both are still actively updating it probably doesn't matter too much which one is killed, however killing off the older of the processes would make sense as that is the orphaned one.

Top
#163976 - 2006-07-05 10:00 AM Re: Killing Orphaned Processes
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
This example kills all but the most recent CMD.EXE on the local "." computer.

Test it by starting a few console sessions, then run the script - from the most recent console of course!

Code:
Break ON
$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")

Dim $sComputer,$sExe
Dim $oWMI,$cProcesses,$oProcess,$sProcessDate,$oProcessHold

$sComputer="."
$sExe="CMD.EXE"

$oWMI=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"+$sComputer+'\root\cimv2')
If @ERROR or VarType($oWMI)<>9 "Cannot create WMI object: ["+@ERROR+"] "+@SERROR+@CRLF Exit @ERROR EndIf
$cProcesses=$oWMI.ExecQuery("select * from Win32_Process where Name=" +'"'+$sEXE+'"')
If @ERROR or VarType($cProcesses)<>9 "Cannot create process collection: ["+@ERROR+"] "+@SERROR+@CRLF Exit @ERROR EndIf

For Each $oProcess in $cProcesses
If $sProcessDate=""
$oProcessHold=$oProcess
$sProcessDate=$oProcess.CreationDate
Else
If $oProcess.CreationDate > $sProcessDate
"Killing "+$oProcessHold.ProcessID+" which started at "+$oProcessHold.CreationDate+@CRLF
$oProcessHold=$oProcess
$sProcessDate=$oProcess.CreationDate
$=$oProcessHold.Terminate
If @ERROR "**ERROR**: Could not kill process ["+@ERROR+"] "+@SERROR+@CRLF EndIf
Else
"Killing "+$oProcess.ProcessID+" which started at "+$oProcess.CreationDate+@CRLF
$=$oProcess.Terminate
If @ERROR "**ERROR**: Could not kill process ["+@ERROR+"] "+@SERROR+@CRLF EndIf
EndIf
EndIf
Next

"Will not kill process "+$oProcessHold.ProcessID+" which started at "+$oProcessHold.CreationDate+@CRLF

Exit 0


Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1188 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.059 seconds in which 0.029 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org