scripting around ps(1) (was: "Strong Scripting Skills" - a definition? - sunday)

Rob Windsor windsor at warthog.com
Wed Jan 28 08:45:21 PST 2004


On Tue, 27 Jan 2004 14:50:57 PST, verily did "Roy S. Rapoport" write:

>>>> defang  7665   212  0 07:03:02 ?        0:14 /bin/perl -w mimedefang.pl
>>>> -server

>>>> This is the problem with choosing a field.  Hint, look at the process
>>>> start time.  This maybe avoided with options to ps, I haven't looked.
>>>> This is "ps -ef" on Solaris.  The Linux "ps auxw" and "ps -ef" the
>>>> start time would be "Oct31", so, it is not an issue there.

> This is why, when I had to do this on solaris, I wrote my own program that
> just accessed the /proc filesystem.  Actually, it was really rather
> anti-social.  It:
> A) nice -20'ed
> B) Killed the right processes (most of the time :) )
> C) Exited.

> In other words, my answer to "how would you script this?" was "no." :)

I decided rather early that ps(1) isn't portable and worked around it.
This decision has worked quite well for me.  :-)

The biggest issue in scripting against process name is the problem of
silly processes that futz with their name in the kernel process table.
sendmail and screen come to mind.

But back to ps(1) issues...

Here's a code snippit from my home-brewed "nkill" ksh script.

I only set it up for NetBSD/Solaris since those were the two OSen that
I needed it on.  It's easy to tweak for a new OS, as you can see.  I'll
tweak it for RedHat (7.x and ES 3.0) soon.  :/

====
OS="`uname -s`"
# [...]
if [ "${OS}" = "NetBSD" ]; then
    PSFLAGS="-axo pid,command"
    ONEPSFLAGS="-xu -p"
elif [ "${OS}" = "SunOS" ]; then
    PSFLAGS="-eo pid,comm"
    ONEPSFLAGS="-f -p"
fi

# [...]

KILLPROC=`ps ${PSFLAGS} | awk .....`
for PROCESSNUM in ${KILLPROC} ; do
    ps ${ONEPSFLAGS} ${PROCESSNUM} | egrep -v 'TIME' | cut -c'1-80'
    [ -z "${DEBUG}" ] && /bin/kill ${SIGNAL} ${PROCESSNUM}
done
====

The entire script is at ftp://ftp.warthog.com/pub/warthog/scripts/nkill.

Before you cronies start pointing out issues in my script, I acknowledge
that there are some gross inefficiencies, and multiple awk lines could
be consolidated into one (see my earlier comment about "looking like C
code").  :-)

(you should see my .profile!)

Rob++
----------------------------------------
Internet: windsor at warthog.com                             __o
Life: Rob at Carrollton.Texas.USA.Earth                    _`\<,_
                                                       (_)/ (_)
"They couldn't hit an elephant at this distance."
  -- Major General John Sedgwick



More information about the Baylisa mailing list