Re: pkill equivalent? (639 Views)
Super Advisor
Posts: 1,138
Registered: ‎05-20-2005
Message 1 of 9 (639 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.

Honored Contributor
Posts: 13,843
Registered: ‎06-21-2000
Message 2 of 9 (639 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.
Acclaimed Contributor
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 9 (639 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:


UNIX95=1 ps -C ${COMM} -o pid='' | while read P
kill -15 ${P}
If it ain't broke, I can fix that.
Honored Contributor
Posts: 1,605
Registered: ‎08-12-2004
Message 4 of 9 (639 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???"
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (639 Views)

Re: pkill equivalent?


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!



Trusted Contributor
Posts: 137
Registered: ‎11-26-2002
Message 6 of 9 (639 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.

#pkill - kill processes matching pattern

prgName=$(basename $0)

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

unset procs_only

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

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

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
echo "$prgName: \"$*\" no matching processes found."

Trusted Contributor
Posts: 137
Registered: ‎11-26-2002
Message 7 of 9 (639 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"

Acclaimed Contributor
Posts: 25,666
Registered: ‎03-06-2006
Message 8 of 9 (639 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
Exalted Contributor
Posts: 33,806
Registered: ‎08-15-2002
Message 9 of 9 (639 Views)

Re: pkill equivalent?


Steven E Protter
Owner of ISN Corporation
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.