Monday, September 5, 2011

shell script to kill a process by name


####################################################
#  Author  : Saurabh Gupta                         #
#  Created : 10:30 AM 9/5/2011                     #
#  Killing a process by its name                   #
#  change your binary name in the scripts and run  #
#                                                  #
#  Usage   : change <your_binary_name> with your   #
#            process or binary name                #
####################################################
#!/bin/bash

YOUR_PROCESS_NAME=your_binary_name



your_binary_name_process_id=`ps -eo pid,args | grep $YOUR_PROCESS_NAME | grep -v grep | cut -c1-6`

if [ "$your_binary_name_process_id" == "" ]; then
     echo "your_binary_name not running"
else
     echo "PID of "$YOUR_PROCESS_NAME " - "$your_binary_name_process_id
     kill -9 $your_binary_name_process_id
     echo "killing your_binary_name"
fi

No comments:

Post a Comment