Pkill.sh [Shell script to kill process by name]

In *nix the "kill" command needs to know the pid(process id) of the process to kill it. So i decided to code a bash script to kill a process by name :

#!/bin/bash

if [ $1 -eq ""]; then
echo "Usage : ./pkill.sh <process name>"
else
get_proc=`ps -e -o pid,command | grep $1`
echo $get_proc > get_it
get_pid=`gawk -F" " '{ print $1 }' get_it`
kill -9 $get_pid
fi

The script is quite messy and certainly needs more improvement.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Need script to kill process of esdemo,eslm,mfds

Hi,
Can any1 help me to get the scipt for killing process of esdemo, eslm, fds i.e microfocus in linux

we generally use-
ps -ef | grep -i esdemo
ps -ef | grep eslm
ps -ef | grep mfds

Sandeep

simple ways

simpler ways I found...

#!/bin/bash
if [ $1 -eq "" ] then
echo "Usage : ./pkill.sh <process name>"
else
for i in `ps ax | grep $1| cut -d ' ' -f 1`
do
  kill -9 $i
done
fi

or direct one liner...

killall <processname> 

Cheerzz
Joviano Dias
jovistruck@yahoo.com
Goa - India

may not always work

I tried to use your code in a script I wrote today and ran into a problem.
When `ps ax` prints something like this:

 9998 ?        S      0:00 victim-process
10025 ?        S      0:09 victim-process

your script cuts the first line to "" or " " and the second line to "10025", thus not killing the first process.

I fixed this problem by using the following code:

for i in `ps ax | grep $1 | grep -v grep | sed 's/ *//' | sed 's/[^0-9].*//'`
do
  kill -9 $i
done

code brackets

Himanshu,

Use the "&#91;" and "&#93;" for the opening and closing square code brackets, "&lt;" and "&gt;" for the angle brackets. Also enclose scripts with <pre> tag. Thanks for your informative posts

--
Sandip

Sandeep bro, Thanks. I

Sandeep bro,
Thanks. I will keep that in mind while posting code from now on.

Post new comment

  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
Comment