[comp.unix.questions] Killing with awk and grep

tgardenh@nmsu.edu (Tricia Gardenhire) (08/11/89)

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. 

Please email responses to tgardenh@nmsu.edu and I'll summarize if
there is enough interest.

Thanks, in advance, for any and all help.

-Tricia

usenet@cps3xx.UUCP (Usenet file owner) (08/11/89)

in article <303@opus.NMSU.EDU>, tgardenh@nmsu.edu (Tricia Gardenhire) says:
> 
> 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

ps -axe | grep pattern | grep -v grep | awk '{print "kill -9 " $1}' | sh


Robert Raisch - TechnoJunkie & UnixNut| UseNet: {uunet,mailrus}!frith!raisch
Network Software Group-301 Comp.Center| InterNet: raisch@frith.egr.msu.edu 
Michigan State University, E. Lansing | ICBMNet: 084 28 50 W / 42 43 29 N
----- The meek WILL inherit the Earth, (Some of us have other plans). ------

ccel@chance.uucp (CCEL) (08/12/89)

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

santhosh@walt.cc.utexas.edu (Santhosh S. Cheeniyil) (08/15/89)

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. 

Try the following:

#/bin/csh
kill -9 `ps | grep sleeper | grep -v grep | awk '{print $1}'`

>-Tricia
Cheers,
--santhosh (Santhosh S. Cheeniyil)
--santhosh (Santhosh S. Cheeniyil)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		If I don't drive around the park,  
		I'm pretty sure to make my mark. 
		If I'm in bed each night by ten,   
		I may get back my looks again.  
		If I abstain from fun and such,  

bob@wyse.wyse.com (Bob McGowen Wyse Technology Training) (08/15/89)

In article <4128@cps3xx.UUCP> usenet@cps3xx.UUCP (Usenet file owner) writes:
>in article <303@opus.NMSU.EDU>, tgardenh@nmsu.edu (Tricia Gardenhire) says:
>> 
>> Hi, I've been reading the man pages for awk, but they just aren't that
---deleted description---
>ps -axe | grep pattern | grep -v grep | awk '{print "kill -9 " $1}' | sh
>
>
>Robert Raisch - TechnoJunkie & UnixNut| UseNet: {uunet,mailrus}!frith!raisch

Why not let awk do all the pattern processing:

ps -aux | awk '$10 ~ /^-sleeper$/{print "kill -9 " $2}' | sh

Using the -aux as in the original question puts the user name in the first
field, the PID in the second.  The tenth field is the first part of the
command, or command name.  The tilde tells awk that the 10th field must
match the pattern as given beginning to end (of the FIELD).  The awk line
is therefor excluded because on it the tenth field is: awk

Bob McGowan  (standard disclaimer, these are my own ...)
Customer Education, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

jes@mbio.med.upenn.edu (Joe Smith) (08/15/89)

> Why not let awk do all the pattern processing:
> ps -aux | awk '$10 ~ /^-sleeper$/{print "kill -9 " $2}' | sh

A good idea, but you will get burned doing this (I know, I have
scorches to prove it!).  Ps is not particularly nice about making it's
output easy to parse.  Some fields have spaces; and, under some
circumstances, others may run together (i.e., no space in between).
Also I think ps output differs among different Unix flavors (I'm only
familiar with BSD).  All this means that using the default awk fields
is a Bad Thing.

If you want to use awk, just scan the whole line:

/-sleeper/ && !/awk/ { print $2 }	# field 2 (pid) is (always?) ok

If you want to look at fields, you'll have to use the substring
function (only in new awk) to break up the lines, but beware, it won't
be portable.

Not that this topic is worth a lot of discussion, but I'd hate to see
someone else get burned on this.

<Joe
--
 Joe Smith
 University of Pennsylvania                    jes@mbio.med.upenn.edu
 Dept. of Biochemistry and Biophysics          (215) 898-8348
 Philadelphia, PA 19104-6059

jmr@motown.UUCP (John M. Ritter) (08/16/89)

>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. 
>
>Try the following:
>
>#/bin/csh
>kill -9 `ps | grep sleeper | grep -v grep | awk '{print $1}'`

try using as few commands as possible. remember, awk can do its own
pattern matching. "ps" won't show arguments to commands without a "-f"
option, therefore,

	kill `ps | awk '/-sleeper/ {print $1}'`

works on my *nix systems.
------------------------------------------------------------------------------
"I enjoy working with human beings, and                         John M. Ritter
have stimulating relationships with them."                 Allied-Signal, Inc.
                            - HAL 9000                Corporate Tax Department
jmr@motown.Allied.COM            {bellcore,clyde,princeton,rutgers}!motown!jmr
------------------------------------------------------------------------------

dg@lakart.UUCP (David Goodenough) (08/16/89)

From article <63247@linus.UUCP>, by ccel@chance.uucp (CCEL):
> 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).

Well, I don't know about your ps, but ours sorts by control tty first. So
you don't really know which one will show up. Also, what happens in a case
of PID rollover: the sleeper you're after is PID 29999, and the grep is
(say) 105. A far better replacement for the ..... | line | ..... is this:

..... | grep -v grep | .....

(assuming you have a civilized grep that knows about -v)
-- 
	dg@lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@xait.xerox.com		  	  +---+