Re: pkill equivalent? (533 Views)
Reply
Super Advisor
dictum9
Posts: 1,127
Registered: ‎05-20-2005
Message 1 of 9 (533 Views)
Accepted Solution

pkill equivalent?


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


Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,752
Registered: ‎06-21-2000
Message 2 of 9 (533 Views)

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.
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 9 (533 Views)

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.
Please use plain text.
Honored Contributor
Court Campbell
Posts: 1,605
Registered: ‎08-12-2004
Message 4 of 9 (533 Views)

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???"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (533 Views)

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...
Please use plain text.
Trusted Contributor
Eric Raeburn
Posts: 137
Registered: ‎11-26-2002
Message 6 of 9 (533 Views)

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
Please use plain text.
Trusted Contributor
Eric Raeburn
Posts: 137
Registered: ‎11-26-2002
Message 7 of 9 (533 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,962
Registered: ‎03-06-2006
Message 8 of 9 (533 Views)

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
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 9 of 9 (533 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation