Operating System - HP-UX
1752277 Members
4604 Online
108786 Solutions
New Discussion юеВ

Re: kill a process by name

 
Bill Booth
New Member

kill a process by name

I would like to be able to kill processes running on HP-UX 10.2.x by
specifiying the name of the process. I did this on earlier versions of HP-UX
by grep'ing the 'ps -ef' listing, stripping out the grep command, cutting the
PID from the resulting line, then issuing a kill on that PID. I did a ?wc ?l?
of the lines, and would kill if at least 1 result was found. This does not
seem to work as well as it used to. The $Count does not always "see" the
process, even though it *is* running. Is there a better way to do this?
Thanks.
2 REPLIES 2
Neil Gast_1
Frequent Advisor

Re: kill a process by name

Here's a script called killem that does what you're looking for:

#!/bin/sh
ps -ef|grep $1|grep -v grep|grep -v killem|
while read X; do
PID=`echo $X|awk '{print $2}'`
kill $PID
done

The script takes one command line arg: the name of the process to look for, and
kills ALL instances it finds.
If you change the name of the script, also change the name on the grep line of
the script. The pipe at the end of that line is not a typo.

Be careful to specify enough of the name to avoid conflicts. You probably don't
want to do things like "killem sh," which would kill all user shells and
anything else that matches "sh".

MrNeil
Paul Heffels_1
Frequent Advisor

Re: kill a process by name

Hi Bill,

I have attached one of the first scripts I've ever wrote to kill processes. It
will do a search for the process given as a parameter to the script and ask you
which one to kill if more than one process matches the search. Please, PLEASE
don't mind the dirty style :-)

Paul "Jeez this script could destroy my reputation" Heffels

PS Never mind the Dutch comments