Operating System - HP-UX
1753455 Members
6232 Online
108794 Solutions
New Discussion юеВ

Re: I'm looking for a perl script to kill a process

 
SOLVED
Go to solution
'chris'
Super Advisor

I'm looking for a perl script to kill a process

hi

has someone a perl script to look if a process
is running and if it is, then kill it ?

kind regards
chris
8 REPLIES 8
Mel Burslan
Honored Contributor
Solution

Re: I'm looking for a perl script to kill a process

perl can call system functions, so something like this should be able to kill anything matching the "PATTERN" in ps -ef output.

perl -e '$cc = system("ps -ef|grep PATTERN|grep -v grep| awk {'print $2'}|xargs kill"); exit($cc);'

take it with a lot of salt as I am not an expert in PERL programming. Know just nuff to get myself in trouble.
________________________________
UNIX because I majored in cryptology...
Ralph Grothe
Honored Contributor

Re: I'm looking for a perl script to kill a process

Hi Chris,

maybe the best (most Perlish) option would be if you installed Proc::ProcessTable from CPAN.
http://search.cpan.org/~durist/Proc-ProcessTable-0.40/ProcessTable.pm

With this module you can scan your system's process table at your heart's content without having to call external programs like ps.

e.g.

Here I retrieve the PID of the dsmc process (i.e. ADMS client)


$ perl -MProc::ProcessTable -le '$pid=(grep $_->{cmd}=~/dsmc/,@{Proc::ProcessTable->new->table})[0]->{pid};print$pid'


Instead of printing you would send it any signal.
You can use Perl's built-in kill() function to this end.

e.g.

kill 15 => $pid;

To send it a SIGTERM.
You can send a whole bunch of processes (you have retrieved from the proc table before) a signal in one go if you pass kill() a list of PIDs.

See "perldoc -f kill"

Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: I'm looking for a perl script to kill a process

Oh forgot,
if you want to avoid the hassle of installing the extra Proc::ProcessTable you can of course also hand the parsing over to the external ps command.

e.g.

$ perl -le 'chomp($pid=qx(UNIX95= /usr/bin/ps -o pid= -C dsmc));print$pid'
13557
Madness, thy name is system administration
Muthukumar_5
Honored Contributor

Re: I'm looking for a perl script to kill a process

Why you are trying to put a shell line into a perl or writing a perl code and executing it in shell prompt. Try simply in the command line itself as,

kill -9 `ps -ef | grep 'PATTERN' | awk '!/grep/ { print $2 }'`

or

PID=$(ps -ef | grep -v grep | grep -q 'PATTERN')
if [[ $PID != "" ]]
then
kill -15 $PID
fi

hth.
Easy to suggest when don't know about the problem!
H.Merijn Brand (procura
Honored Contributor

Re: I'm looking for a perl script to kill a process

Mel, though that *lloks* like perl, it in fact is just a perl wrapper around one single system command, one that calls grep and awk. Urgh!

*IF* you use backticked ps (or system ()), *PLEASE* use perl's builtin functions to process further

# perl -e 'kill 15,map{m/^\s*\w+\s+(\d+)/;$1}grep/pattern/,`ps -ef`'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Gordon  Morrison
Trusted Contributor

Re: I'm looking for a perl script to kill a process

I've said it before, I'll say it again.
Perls are nice, ksh is a gem:o)

I wrote the attached script some time ago to repeatedly kill self-respawning processes that were running amok & getting in the way.

It works, and is well commented, but do please read the warning.

Share And Enjoy
What does this button do?
'chris'
Super Advisor

Re: I'm looking for a perl script to kill a process

thanks to ALL

@prokura
" *IF* you use backticked ps (or system ()), *PLEASE* use perl's builtin functions to process further "

can you pls give more details ?

and this won't work on my system running as root from command line:

# perl -e 'kill 15,map{m/^\s*\w+\s+(\d+)/;$1}grep/ntop/,`ps -ef`'
ps: Process environment requires procfs(5)

or do I something wrong ?
Ralph Grothe
Honored Contributor

Re: I'm looking for a perl script to kill a process

Hm, procfs to my knowing is typically Linux
(or Solaris, albeit a bit more limitted).
Are you trying this on a Linux box?
This should also explain your usage of killall in another of your threads.
Btw, have you noticed that ITRC also maintains a Linux forum?
Chances for a satisfactory answer there might be higher.
Madness, thy name is system administration