shell script to find a process by name and kill it

hi,

Am a newbie to unix and wasnt able to write script to my requirement.

I need a shell script, which should find a process by name and kill it. For eg: let the process name be "abc". I have different processes running by this name(abc), so should kill them all.

Condition would be: if this process(abc) is running, display a comment "process is running" and also kill all the processes associated with this name.
else
display a comment "process is not running" and then move to next step.

Thanks in Advance.

#!/bin/bash
pgrep $1 2>&1 > /dev/null
if [ $? -eq 0 ]
then
{
echo "process running"
ps -ef | grep $1 | grep -v grep | awk '{print $2}'| xargs kill -9
}
else
{
echo "no process running"
};fi

run:

./killscript.sh your_process_name

What OS and shell are you using? ex: Solaris has pkill which does exactly what you want

hey Chidori,

thanks for that script, as I told you am newbie to unix, can you put that "abc" process in your example and edit the code. As I dnt find anywhere to put my process name in that code.

---------- Post updated at 02:24 PM ---------- Previous update was at 02:23 PM ----------

am using Solaris

SunOS 5.10 Generic_142900-14 sun4u sparc SUNW,Sun-Fire-V245

The $1 and $2 are commandline parameters, you put them after the program when you run it.

you can use the script to kill any process... just run like this

./killscript abc

I already have a server script in place, I want to add this to existing script so that "abc" process gets killed when the main scripts executes.

replace $1 value of my script with your "abc" process

pgrep abc 2>&1 > /dev/null 
if [ $? -eq 0 ] 
then 
{ 
echo "process running" 
ps -ef | grep abc | grep -v grep | awk '{print $2}'| xargs kill -9 
} 
else 
{ 
echo "no process running" 
};fi