1748250 Members
3543 Online
108760 Solutions
New Discussion юеВ

Re: pkill equivalent?

 
SOLVED
Go to solution
dictum9
Super Advisor

pkill equivalent?


What is the hp-ux equivalent of sun's pkill? I cannot find pkill anywhere on the 11.23 system.


8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: pkill equivalent?

What does pkill on Solaris do? If we have an idea of the function, we may be able to suggest an HP-UX equivalent.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: pkill equivalent?

There is no direct equivalent for this non-standard command but it is very easy to craft an equivalent leveraging the XPG4 behavior of ps:

#!/usr/bin/sh

COMM=${1}
shift
UNIX95=1 ps -C ${COMM} -o pid='' | while read P
do
kill -15 ${P}
done
If it ain't broke, I can fix that.
Court Campbell
Honored Contributor

Re: pkill equivalent?

you are going to have to get the pid(s) of the process(es) you want to kill and then use the kill command to terminate the process(es). Or you could write your own script that works like pkill.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
James R. Ferguson
Acclaimed Contributor

Re: pkill equivalent?

Hi:

There isn't a direct counterpart. However, you can/should leverage the UNIX95 behavior of 'ps' to find a process by name :

# UNIX95= ps -C cron -o pid=

For example, the above will return the 'pid' of the 'cron' daemon. The match is *exactly* to the basename of a process and no other. The '-o pid=' signifies that all you want shown is the 'pid'(s) of processes by the name of the '-C' switch's argument.

The equal sign after the 'pid' argument means suppress the heading line from 'ps'.

There is a space (or tab) after UNIX95=. This limits the UNIX95 behavior to the command line only.

Given that you find the correct pid for the correct process, you can proceed to gently 'kill' the process. That is, begin with a simple 'kill -hup' and then a 'kill -term' and *only* as a last resort, a 'kill -9'. Remember that at 'kill -9' cannot be trapped and may leave shared memory segments and/or temporary files abandoned!

Regards!

...JRF...
Regards!

...JRF...
Eric Raeburn
Trusted Contributor

Re: pkill equivalent?

Here you go. I've been using this for years. It will show you what it's going to kill and prompt for your approval.


#!/usr/bin/sh
#pkill - kill processes matching pattern

prgName=$(basename $0)
TMPFILE=/tmp/$prgName.$$

syntax()
{
cat << EOH
Usage:
pkill [-p] [-s sig] pattern
Description:
Kill all processes in output of 'ps -ef' that match pattern.
Options:
-p Match pattern only to process names (use output
of 'ps -e').
-s sig Send this signal; if not specified, -9 is used
EOH
exit
}

unset procs_only
sig=-9

while getopts ps: opt ; do
case $opt in
p) procs_only=TRUE ;;
s) sig=$OPTARG ;;
*) syntax ;;
esac
done
shift $(( $OPTIND-1 ))
[ -z "$*" ] && syntax

if [ -n "$procs_only" ] ; then
ps -e | grep "$*" | nl -s":" > $TMPFILE
else
ps -ef | grep "$*" | egrep -v "grep|$prgName" > $TMPFILE
fi

if [ -s $TMPFILE ] ; then
sed 's/ / /g' $TMPFILE
echo "Send kill $sig? Press [Ctl][C] to abort, [Enter] to procede >\c"
read a
cat $TMPFILE | awk '{ print $2 }' | xargs kill $sig
else
echo "$prgName: \"$*\" no matching processes found."
fi

rm $TMPFILE
Eric Raeburn
Trusted Contributor

Re: pkill equivalent?

Actually, my script would be better if it piped ps through egrep instead of grep. Then it could be used to kill process names matching more than one pattern:

pkill "this|that|the_other"

-Eric
Dennis Handly
Acclaimed Contributor

Re: pkill equivalent?

pkill is available on 11.31.

>Eric: my script would be better if it piped ps through egrep instead of grep.
pkill "this|that|the_other"

There is no reason to use an egrep hammer for this. Unless you want that interface in your script. (It appears you might.)

You can simply use: grep -e this -e that -e the_other
Steven E. Protter
Exalted Contributor

Re: pkill equivalent?

Shalom,

http://www.hpux.ws/gkill

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com