Killing with awk and grep

CCEL ccel at chance.uucp
Sat Aug 12 03:34:44 AEST 1989


In article <303 at opus.NMSU.EDU> tgardenh at nmsu.edu (Tricia Gardenhire) writes:
>Hi, I've been reading the man pages for awk, but they just aren't that
>helpful.  So here is my question:  I want to create a shell script
>that will look at ps -aux for a certain process called '-sleeper' and
>then kill it.  I've figured out how to search for it using grep and
>how to display the PID with awk.  But, I have no idea how to use these
>with kill in mind.  Something else I'm sure you will know, how do I
>keep the script from killing itself?  Grep will find everything with
>the word '-sleeper' including the grep command finding the word.
>Any ideas.

(I would e-mail you the answer, but my mailer has about a 35% success
rate. Sorry to all of those that really didn't want to read this msg.)

Funny, we wrote something that did exactly this for one of our
applications. To be fun, I did it in one line:

kill -9 `ps -ax | grep 'sleeper' | line | awk '{ print $1 }' `

Ok ... ps -ax lists the processess, grep finds all occurences of 'sleeper'
(there should be two ... the actual process, and your grep call). line
will just return the first line (since sleeper already exists when you
type this in, it will be first in the ps -ax listing). That awk command
will just return the first word of what is given to it.

Putting that mess in back quotes (` `) will pass it to the shell to be
used as an argument to kill.

Enjoy.
Randy Tidd                   MITRE-McLean CCEL Lab
rtidd at mitre.arpa             ccel%community-chest at gateway.mitre.org
#define DISCLAIM TRUE



More information about the Comp.unix.questions mailing list