Re: process control

From: Alexis Huxley (alexis_at_dione.no-ip.org)
Date: 07/31/03

  • Next message: scriptOmatic: "Re: awk & shell variables"
    Date: Thu, 31 Jul 2003 00:18:39 +0200
    
    

    In article <vbXVa.1469$wV5.22@newssvr22.news.prodigy.com>, Leiji Liu wrote:
    >
    > #!/bin/ksh
    > check=`ps -ef |grep "/bin/ksh /home/mycode/allrun.sh" | wc -l`
    > if [ $check = 2 ] ; then
    > process my programs
    > fi
    >
    > Here is the question. Why should the $check be 2? Or should it be better
    > $check < 2? When I manually start "allrun.sh" (not from cron table) from
    > command line, the $check is 0. Can anybody give me some explanation?

    Both Solaris and HP-UX (and possibly others) seem extremely variable
    in the number of processes they report in this situation. I would
    *expect* that the correct number of processes containing the
    grep pattern is (a) one for the script itself (b) one for the pattern occurring
    on the grep commandline which ps will see and grep will grep out
    (c) possibly one more for the backquotes creating a subshell (but I
    think this is implementation specific), but then probably remove (b) because
    the ps output is probably not wide enough to show the whole grep
    pattern so grep won't find it in the ps listing after all. So in
    total 2.

    But as I said, in my experience this number varies (ps timing? race
    conditions?) and it is not a reliable method to test if a process is
    running. Better to put the PID of the running process in a file with
    well known location:

            #!/bin/ksh
            PROGNAME=`basename $0` # yes, ksh can do this nicer ways
            echo $$ > /var/locks/$PROGNAME.pid

    but actually do this only after it has tested to see if the file exists
    already and corresponds to a running PID (not checked!):

            #!/bin/ksh
            PROGNAME=`basename $0` # yes, ksh can do this nicer ways
                                            # throw away errors caused by non-existent file
            PREVIOUS_PID=`cat /var/locks/$PROGNAME.pid 2>/dev/null`

                                            # if have old pid and is running abort
            [ "X$PREVIOUS_PID" != X -a [ "X`ps -ef | grep \" $PREVIOUS_PID \"`" != X ] && {
                    echo "$PROGNAME: ERROR: locked!" >&2
                    exit 1
            }
                                            # this is nicer way to do the same but less
                                            # portable (-f or -d? check!)
            [ "X$PREVIOUS_PID" != X -a -f /proc/$PREVIOUS_PID ] && {
                    echo "$PROGNAME: ERROR: locked!" >&2
                    exit 1
            }
            }
            echo $$ > /var/locks/$PROGNAME.pid

    There is clearly a race condition possible here (second invocation
    starts up between us seeing no lock file and us creating one). Search
    comp.unix.shell archives for extensive discussion on how to
    "test for old locks and insert new ones" in an indivisible step.

    --
    Alexis
    http://dione.no-ip.org/~alexis/
    

  • Next message: scriptOmatic: "Re: awk & shell variables"

    Relevant Pages

    • Re: grep and operator
      ... Subject: grep 'and operator' ... grep 'first pattern' and 'second pattern' foofile ... If BRP always appears at the beginning of the line, or rather, the BRP you ... record delimiter and so you may want to switch to awk where you can specify ...
      (comp.unix.sco.misc)
    • Re: Search & Replace
      ... Seems simple enough but I had to fiddle about for ages ... GREP box or not. ... but only if you include it in the pattern. ... TW's excellent colour-coded search and replace fields make it /relatively/ straightforward to spot special characters. ...
      (comp.sys.mac.apps)
    • Re: stat and its exceptions
      ... match the pattern, ... Please read perldoc perlstyle. ... It's not in the FAQs, ... > I know the grep loads everything. ...
      (comp.lang.perl.misc)
    • Re: All the world seems in tune
      ... both of which use 2-byte pointers into the pattern, ... runs into this limit in grep, and sometimes it's a Real Pain In The ... I'm not going to rewrite it right away. ... fiber splicing kit for the tiny tots to have Fun with just like Daddy (but ...
      (alt.sysadmin.recovery)
    • Re: Different way of getting Standard input
      ... If there are entries in @ARGV (the variable that holds what ... (e.g. grep, wc, sort, etc). ... die "$0 needs a pattern to match with" unless @ARGV; ... cowens@amans:~$ perl grep.pl perl grep.pl ...
      (perl.beginners)