killing a process from a script

Hey all. I'm brand new to this forum and am looking for some help. I have a script that verifies that the backup tapes are working correctly. Basically is uses 1 command: restore -xpqvf > rootvglog

I use this for each volume group that we have. We run this everyday but the problem is, we found processes out there that have been running for months slowing the system down. So I want to add a check to make sure that the process is killed before we start another.

I would like to use ps -eaf |grep restbyname. The only problem is, I don't know how to implement this into my script. It always returns 1 row (the grep) but I'm not sure how to create the if statement to get this to kill the correct process. Can anyone help?

ps -eaf |grep [r]estbyname

What you need is cut. "grep" only gives you the lines you are interested in, cut gives you the part you need to kill the process. Look at the output of "ps -ef", you see a table. To kill a process you need the PID, which is the 2nd column. Therefore, the solution is to first cut out all lines you don't need (processes you don't want to kill), then filter out the PIDs of the remaining lines, then feed this list to a for- or while-loop to do the killing:

ps -ef | grep <processname> | sed 's/<blank><blank>*/<blank>/g' | cut -d'<blank>' -f2 |\
while read PID ; do
kill -9 $PID
done

The sed-command is used to replace all multiple occurencies of <blank> (replace in your script by a single space) by a single whitespace since 'cut' uses <blank> as a delimiter (-d).

Hope this helps.

bakunin

or my personal favorite ... will kill all instances of restbyname ...

kill -9 `ps -ef | awk '/restbyname/ && !/awk/ {print $2}'`

That one seems to work for me while this one (that I use on other servers/instances) doesn't. The don't look all that different...
ps -ef | grep NO | grep oasisrpt | awk '{print "kill -9 " $2 $7}' > killem1.sh

This one (modelled after yours), seems to work...
kill -9 `ps -ef | grep NO | awk '/oasisrpt/ && !/awk/ {print $2}'`

?????