Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!network!ucsd!orion.cf.uci.edu!uci-ics!zardoz!tgate!ka3ovk!drilex!axiom!linus!chance!ccel From: ccel@chance.uucp (CCEL) Newsgroups: comp.unix.questions Subject: Re: Killing with awk and grep Message-ID: <63247@linus.UUCP> Date: 11 Aug 89 17:34:44 GMT Sender: news@linus.UUCP Reply-To: rtidd@mitre.arpa Organization: MITRE-McLean Software Engineering Laboratory Lines: 32 In article <303@opus.NMSU.EDU> tgardenh@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@mitre.arpa ccel%community-chest@gateway.mitre.org #define DISCLAIM TRUE