Operating System - HP-UX
1833757 Members
2260 Online
110063 Solutions
New Discussion

Killing a process by command name

 
SOLVED
Go to solution
Tom Grill
Occasional Contributor

Killing a process by command name

Is there a simple method to kill a process by it's command name that could be run as a cronjob? There could possibly be multiple instances of the same command name.
7 REPLIES 7
Pete Randall
Outstanding Contributor
Solution

Re: Killing a process by command name

Tom,

You can set up a script like this to be placed in cron:

ps -ef |grep "command" |grep -v "grep" |awk '{ print $2 }' > /tmp/killlist
for i in `cat /tmp/killlist`
do
kill $i
sleep 1
kill -9 $i
done


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Killing a process by command name

Hi Tom:

To do this safely do this:

# TASK=/usr/bin/sleep #...for example, only!

# PIDS=`UNIX95= ps -C ${TASK##*/} -o pid='`
if [ ! -z "${PIDS}" ]; then
kill -1 ${PIDS} > /dev/null 2>&1
sleep 3
kill -15 ${PIDS} > /dev/null 2>&1
sleep 3
kill -9 ${PIDS} > /dev/null 2>&1
fi

The UNIX95 variation with the '-C' switch finds *only* the prcoess with a basename that matches its argument.

The three-level kill gives the process a chance to catch the signal and cleanup temporary files and/or shared memory segements.

Regards!

...JRF...

Raj D.
Honored Contributor

Re: Killing a process by command name

Hi Tom ,

You can do simply with this :

# PID= $(ps -ef | grep "command" | grep -v grep | awk '{print $2}') > kill.list

#
for i in `cat kill.list`
do
kill -SIGHUP $i
echo "sent SIGHUP to PID = $i ."
sleep 1
kill -9 $i
echo "sent SIGKILL to PID = $i ."
done


You can include in a script and put that in a cron to execute as per need.


Hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Killing a process by command name

Correction pls:
instead of PID= its :

# ps -ef | grep "command" | grep -v grep | awk '{print $2}' > kill.list

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
A. Clay Stephenson
Acclaimed Contributor

Re: Killing a process by command name

Relying on grep for this is dangerous although grep -E with some careful anchoring will work. The problem is suppose you are trying to kill "myproc". Well "myproc" will match but also "notmyproc", "myproc23", etc. will also match using grep. A better approach is to extract just the process name and use it.

#!/usr/bin/sh

typeset TARGET=${1} # process to kill
shift
typeset -i PID=0
typeset PROC=''

ps -e | awk '{print $1,$NF}' | while read PID PROC
do
if [[ "${PROC}" = "${TARGET}" ]]
then
kill -15 ${PID}
fi
done

I am assuming that the process you are not trying kill is awk; if so then additional logic will be needed.

If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

Re: Killing a process by command name

Whatever you do, do NOT use grep or awk or to search through the ps output!! While you may have a very unique process name for this script, grep and awk do NOT match just the process name. They look at the entire line and this can lead to drastic consequences. The simplest example is trying to kill all processes named "sh". The grep/awk pattern matching will match all occurances of unhasdaemon, bash, ksh. It will also match pathnames such as /usr/local/bin/dash45, and will match userID's such as henshaw and sherry (and kill all their processes!).

ps has a very important but overlooked option: -C proc_name. It is only valid when the temporary variable UNIX95 is set. Compare the output of these two commands:

ps -ef | grep sh
UNIX95=1 ps -f -C sh

So always use ps (not grep or awk or other pattern matching) to find the exact name of a process. You can have multiple -C options to search for several process names.


Bill Hassell, sysadmin
Arturo Galbiati
Esteemed Contributor

Re: Killing a process by command name

Hi,

for PidNbr in $(ps -ef|awk '$1=="user"&&$8="command" {print $2}')
do
kill $PidNbr
done
for PidNbr in $(ps -ef|awk '$1=="user"&&$8="command" {print $2}')
do
kill -9 $PidNbr
done

where user is the UNIX user if the command, and command is the comamnd you want to kill.

HTH,
Art