killing process using a script

can I do
ps -ef | grep <process_name>
and kill the process is it exists?
and send a mail to me that the process was found and killed

Thanks much...

KS

Yes, you can.

Are you asking for one of us to do it for you? If so, you won't learn. Start writing the script in your choice of shell and if you have problems, post back.

Some hints: watch out for duplicate process names
Watch out when searching for a particular pid (if you do it that way) since ps -ef|grep 5445 will also bring you process id 25445.

Check out the following man pages for the commands:
kill
ksh, sh, or (forgive me Perderabo) csh, bash....you will find the appropriate information for creating loops.

Check out the mailx command also.

And search the forum for those commands as you may find others asking the same question that have provided scripts that will help.

I tried the following:

#! /bin/ksh

if test ps -ef | grep <process_name>
killall <process_name>
echo "Found <process_name> and killed process name | mail -s "killed <process_name `date`" skotapal
fi

I did not know how to check for the condition to see if the process exists... I can pipe the output to a file and check for file size... but that would not be elelgant!! :wink:

cheers!!

First, what OS are you running?

In Solaris, killall is used by shutdown to kill all processes. Using it in your script won't be very good if your OS does the same. There is no option to specify the 'process name' to killall (or to kill).

Kill accepts the process id as a parameter. You would have to grep for your processname, and then pull the pid (normally the second field) You can use the awk command to do this (or the cut command). Check the man pages.

Second, I don't believe the mail command has a -s option (this may just be a typo - meaning mailx -s)

My guess is that he's using a Linux box.
The mail that shipd with most distributions uses "-s" for subject.
Also, on Linux system, the killall command can be used to feed a process name to kill, rather than a number.
So "killall sh" will try to kill every process names "sh" running.