[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

OT Shell tricks: I'll kill you later



I have few ugly shell scripts which do a lot of WHOIS queries, which
I've found are prone to hang for a long time.  The simple thing to do is
to hunt for whois queries, then, and periodically kill them:

    while sleep 600
    do
        ps aux | awk '/[w]hois/ {print $2}' | xargs kill
    done

Of course, this is a bit indiscriminate:  it kills any WHOIS process
that happens to be running at the moment.

Best would be to avoid the hung processes at all.  But I haven't figured
that one out yet.

Better, though, is to throw a delay into the processing -- find out
which processes are there, then attempt to kill them later.  *Most*
WHOIS queries return quickly.  Actually, most of 'em return *really*
quickly since I use a caching whois clinet (jwhois, since you're
asking).  But let's say I need a 30 second delay.  Best not to dally
*too* long or I might roll through my PID table.

The inspiration was an old recipie from the O'Reilly _UNIX Power Tools_
book for moving a filesystem with tar (this back before rsync or cp -pdR,
etc., could do this reliably).  Basically, you tar to stdout, then
extract from stdin, from a shell which has been relocated:

    tar cf - . | ( cd /otherdir; tar xvf - )

I found that it didn't work trying to pipe through sleep (I guess the
lesson is:  don't smoke in bed):

    echo foo | sleep 30 | sed -e 's/foo/bar/'

...but piping through a subshell (that's what the () gives you) *does*
work:

    echo foo | ( sleep 3; cat ) | sed -e 's/foo/bar/'

So now I've got:

    while sleep 600
    do
        ps aux | awk '/[w]hois/ {print $2}' | ( sleep 30; cat ) | xargs kill
    done
    

And reading this post as I write it, I realize I've also won myself a
gratuitous use of cat award, so we can further simplify to:

    while sleep 600
    do
        ps aux | awk '/[w]hois/ {print $2}' | ( sleep 30; xargs kill )
    done


Enjoy.


Peace.

-- 
Karsten M. Self <kmself@ix.netcom.com>        http://kmself.home.netcom.com/
 What Part of "Gestalt" don't you understand?
    I mean once word leaks out that a pirate has gone soft, people begin
    to disobey you and it's nothing but work, work, work all the time.
    - Princess Bride

Attachment: signature.asc
Description: Digital signature


Reply to: