Wednesday, July 9, 2008

Linux Killing a Process using pid or process name

To get rid of a background job, use the kill command with the job number as an argument. For example, to kill job 2:

% kill %2

To get rid of only one process, use the kill command with the process ID as an argument. For example, to kill process 12030:

% kill 12030

If that doesn't kill it, use the -9 option:
% kill -9 12030

Here is a ksh script to kill processes by name.
Usage: kill processName


---------------- cut here ---------------------
#!/usr/bin/ksh

str="$1"

ps -ef grep "$str" {
while read aline;do
cmd=`echo $aline awk '{ print $9 }'`
if [ [ $cmd = $0 ] ]; then
continue
fi

cmd=`echo $aline awk '{ print $8 " " $9 " " $10 }'`
if [ [ $cmd = "grep" ] ]; then
continue
fi

cmd2=`echo $cmd grep "$str"`
if [ [ $cmd2 = "" ] ]; then
continue
fi

print "$aline"
pid=`echo $aline awk '{ print $2 }'`
print "kill pid = $pid"
kill -9 $pid
done
}

No comments:

 
Custom Search