Complete.Org: Mailing Lists: Archives: discussion: March 2000:
[aclug-L] Re: script to kill
Home

[aclug-L] Re: script to kill

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: discussion@xxxxxxxxx
Subject: [aclug-L] Re: script to kill
From: Tom Hull <thull@xxxxxxxxxxx>
Date: Wed, 22 Mar 2000 16:30:16 -0600
Reply-to: discussion@xxxxxxxxx

Carl D Cravens wrote:
> 
> On Wed, 22 Mar 2000, Tom Hull wrote:
> 
> > $ ps -e | awk '$4==PROG{print $1}' PROG=init -
> 
> This doesn't produce any output on my box.  (And 'ps e' under Debian 2.0
> has the progname in the fifth field, too.)

The traditional Linux ps command used the BSD options, which are completely
different from the SVR4 ps option. Someone came up with the idea of writing
one program to support both option sets, where the SVR4 options switches
are preceded by - while the BSD options don't start with -. The SVR4 ps -e
lists _every_ process, which is not what ps e does; the corresponding BSD
switch is: ps x

One problem is that ps x sometimes prints out the command name (in $5, as
you note below) surrounded by [...]. Not sure what this means, but it's
probably in the FM. In any case, it's an extra nuissance to sift out.
Looks like this would do:

  ps x | tr -d '[]'

> This works...
> 
> ps e |awk '$5 ~ /PROG/ {print $1}'

This gets around the [] problem, but will produce false positives for
cases where PROG is a substring of $5. You could get an exact match by:

  awk '$5 ~ /^PROG$/ {print $1}'

But that's the same as using ==. You could beat the optional [] with:

  awk '$5 ~ /^\[?PROG\]?$/ {print $1}'

> ...where PROG is the name of the program you're looking for.  (Looking at
> the man page, it looks like == is a numerical operator... I can get it to
> match numbers, but not strings.)

The awk == operator (and other operators) does numeric comparisons if the
operands look like numbers; otherwise it does a string compare. E.g.:

  $ echo "1 1ax" | awk '{ print $1 == $2 }'
  0
  $ echo "1 1.0" | awk '{ print $1 == $2 }'
  1

> I should learn more about awk... this method is a lot simpler than my
> usual...
> 
> ps e | grep PROG | grep -v grep | awk '{print $1}'

BTW, cf. killall(1), which may be the right answer to the original
question (although the man page has some curious caveats).

-- 
/*
 *  Tom Hull * thull@xxxxxxxxxxx * http://www.ocston.org/~thull/
 */

-- This is the discussion@xxxxxxxxx list.  To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]