Monday, January 2, 2012

shell script to kill a process by name

#!/bin/bash
#########################################################################
## Name    :    kill-process-by-name.sh
## Author  :    Saurabh Gupta
## Date    :    PM 07:05 01 January 2012
## Desc    :    kill process by name in unix
## Source  :    http://www.ccplusplus.com/p/shell-scipt.html
## Note    :
#########################################################################

echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo " killing process :- $1"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

if [ "$1" == "" ];then
   echo "Usage : ./pkill.sh <process name>"
else
     for i in `ps ax | grep $1 | grep -v grep | sed 's/ *//' | sed 's/[^0-9].*//'`
        do
           kill -9 $i
        done
fi

No comments:

Post a Comment