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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding PID & killing a process in one command

Status
Not open for further replies.

jakhan

IS-IT--Management
Jul 22, 2000
75
hi

please provide me a command with which i can search and kill in One go

like , currently i have to do this

# ps -ae |grep httpd
then
# kill -9 1345

tell me how to do this in one line

thanks

Jahangir
 
"pidof" -- what's that? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi,
why not make a script out of

# ps -ae |grep httpd
then
# kill -9 1345


PIDS=( `ps -ae |grep $1 | cut -c 10-15` )
kill -kill $PIDS

I am not sure of the column numbers 10-15 since every PS command display is a little different.

The nice thing about making the script is you can put in logic to handle what happens if you find multiple matches for $1. Kill them all or abort the command and ask them to be more specific.
 
Ok
I have a script that should work for you.
#!/bin/ksh
pattern=$1

if [ -z "$pattern" ];
then
clear
echo
echo
echo
echo "Usage : $0 pattern"
echo
echo "Kills all processes which have the given string as pattern"
echo
echo "For example to kill all processes related to mq"
echo
echo "$0 amq"
echo
fi

for pid in `ps -ef | grep $pattern | grep -v grep | awk '{print $2}'`
do
kill -9 $pid
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top