Killing with awk and grep

David Goodenough dg at lakart.UUCP
Thu Aug 17 00:29:31 AEST 1989


>From article <63247 at linus.UUCP>, by ccel at 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 at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.unix.questions mailing list