Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to kill a process by its name

Status
Not open for further replies.

gvidos

Programmer
Jul 24, 2002
17
0
0
GR
Hi does anyone know
how I can kill a process by specifying its name in a shell script?

e.g. with ps -C rsvpd

we get the ps output for that process. How can we kill that process? The first value in the string returned is PID.
can we do

kill -9 pid

??

Also,can we get status of the ps -C (i.e. determine if that process is presently running),and if yes,then kill it?
 
if UNIX95= ps -C "ttisr" | tail -1 | awk '{print $1}' 1>/tmp/dss.out
then
print " exists killing `cat /tmp/dss.out` "

else

print " not exist"
fi

where it has killing you would have your command
kill `cat /tmp/dss.out`


HTH
 
Killing processes is typically done by PID, not by process name. Some flavors of unix have a facility that allows you to kill by name, but killing by PID is the universal way of doing it.

The command is: [tt]kill [signal] ${PID}[/tt]

"signal" can be a variety of values, view the manpage for kill for a detailed description of them.

Normally when you kill a process, you just "kill ${PID}", without the -9. This allows most programs (assuming they are working normally) to shut down properly.

Using -9 is a special signal, it is handled by the OS, not the program itself, and the OS simply removes the process from the run tables.

"kill ${PID}" is akin to using "+++" "ath" on a modem.
"kill -9 ${PID}" is yanking the phone cord out of the wall.

Where possible, it's preferable to not use the -9, but in too many cases, use of it is inevitable.
 
Script killprog kills a program by name instead of by process id. If there is more than one copy, the script asks the user to pick one from a numbered list. Users of different flavors of Unix may have to modify the ps command slightly.

Create the following script

#!/bin/ksh
# killprog: kill a program by name instead of by process id.
# If there is more than one copy, display a list, and
# ask to pick the right one.

Kill=/tmp/killprogout.$$
if [ $# -eq 0 ]
then
printf "Usage: killprog name\n"
exit 1
fi

# eliminate present program and 'grep $1' from process
ps -ef | grep -v "grep $1"|grep -v $0| grep $1 > $Kill
Count=`wc -l $Kill | awk '{ print $1 }'`
if [ $Count -ge 1 ]
then
awk ' { printf &quot;%2d : %s\n&quot;, NR, $0 }' < $Kill
else
printf &quot;No matches to kill\n&quot;
if [ -f $Kill ]
then
rm $Kill
fi
exit 1
fi
printf &quot;Which of the above do you mean: enter number or '0' to quit: &quot;

while true
do
read lineno
# non-numeric data returns 0
lcnt=`echo $lineno|awk ' { if ($0 ~ /[^0-9]/ )
print 0
else
print 1 } '`
if [ $lcnt -eq 0 ]
then
printf &quot;Valid input: 1-$Count or '0' to quit. Reenter number: &quot;
continue
fi

# default to 0 and quit
lineno=${lineno:=0}
if [ $lineno -eq 0 ]
then
if [ -f $Kill ]
then
rm $Kill
fi
exit 1
fi

if [ $lineno -gt $Count -o $lineno -lt 0 ]
then
printf &quot;Valid input: 1-$Count or '0' to quit. Reenter number: &quot;
else
break
fi
done

Flist=`sed -n &quot;${lineno}p&quot; $Kill`
echo $Flist | awk '{ printf &quot;%s\n&quot;, $0 }'
Killproc=`echo $Flist | awk '{ print $2 }'`
printf &quot;Kill ($Killproc) ? y/n &quot;
read answer
case $answer in
y|Y) kill $Killproc
printf &quot;$Killproc terminated!\n&quot;;;
*)
printf &quot;Nothing killed\n&quot;;;
esac
if [ -f $Kill ]
then
rm $Kill
fi
# end killprog

Sample output:

$ killprog vi
1 : fred 12075 11088 0 13:57:14 p0 0:00 vi
2 : fred 12102 11088 0 13:57:41 p0 0:00 vi untab
Which of the above do you mean: enter number or '0' to quit: 1
fred 12075 11088 0 13:57:14 p0 0:00 vi
Kill (12075) y/n ? y
12075 terminated!

Regards Mike
--
| Mike Nixon
| Unix Admin
| ----------------------------
 
ps -ef | grep &quot;your prog_name&quot; | grep -v grep | awk '{print $2}' | xargs kill -9

will kill all the processes with the name &quot;your prog_name&quot; thoughout the system.
To restrict it to processes you have created use

ps -U &quot;your_login_id&quot; | grep &quot;your prog_name&quot; | grep -v grep | awk '{print $1}' | xargs kill -9


There is no harm in sending a kill signal to a process which is already dying,as the signal will be discarded.

&quot;xargs kill -9&quot; takes the list of pids supplied by the pipe before it and sends the kill -9 signal to all the pids.

I am not familiar with the ps -C format but you can easily use the same concepts with it.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top