Wednesday, 24 April 2013

Unix Shell Script: Kill multiple processes of same name


#!/bin/bash
echo "Enter process name (or part of process name)"
read pname

noOfInstances=`ps -ef | grep $pname | grep -v grep | wc -l`
echo $noOfInstances

# ps -ef | grep $pname | grep -v grep | cut -d" " -f2 > ~/Documents/Linux/s-killMultiPs/pnames.log

ps -ef | grep $pname | grep -v grep | awk '{print $2}' > ~/Documents/Linux/s-killMultiPs/pnames.log

while read line
do

echo "Killing PID: " $line
kill -9 $line

done<~/Documents/Linux/s-killMultiPs/pnames.log


Also, you can use pkill command :

$pkill process_name

No comments:

Post a Comment