crontab sh problem

crontab sh problem

Post by Ric Flin » Thu, 08 Feb 1996 04:00:00



I have written a short sh script to check if a process is running and
start it if it isn't. The process looks for a file (which may or may not
exist) that contains the pid, checks to see if the process is running:

if [ -f $pidfile ]; then
        $pid=`cat $pidfile`
        $check=`ps x | grep "^ *$pid"`
        if [ -z "$check" ]; then
                $progname > /dev/null 2>&1
        fi
else
  $progname > /dev/null 2>&1
fi

This works great from the command line, but fails if I put it in the
crontab, and cron doesnt mail me anything.  The system is NetBSD. Any
suggestions?
--

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

 
 
 

crontab sh problem

Post by Bill Marc » Thu, 08 Feb 1996 04:00:00



Quote:>I have written a short sh script to check if a process is running and
>start it if it isn't. The process looks for a file (which may or may not
>exist) that contains the pid, checks to see if the process is running:

>if [ -f $pidfile ]; then
>    $pid=`cat $pidfile`
>    $check=`ps x | grep "^ *$pid"`
>    if [ -z "$check" ]; then
>            $progname > /dev/null 2>&1
>    fi
>else
>  $progname > /dev/null 2>&1
>fi

>This works great from the command line, but fails if I put it in the
>crontab, and cron doesnt mail me anything.  The system is NetBSD. Any
>suggestions?

I'm surprised that it works from the command line.
Quote:>    $pid=`cat $pidfile`
>    $check=`ps x | grep "^ *$pid"`

should be:
        pid=`cat $pidfile`
        check=`ps x | grep "^ *$pid"`
You don't need $ before a variable name when you assign a value to that
variable.  If you do:
        $pid=`cat $pidfile`
creates a new variable whose name is the previous contents of $pid.

 
 
 

crontab sh problem

Post by Marcel Meij » Fri, 09 Feb 1996 04:00:00



> I have written a short sh script to check if a process is running and
> start it if it isn't. The process looks for a file (which may or may not
> exist) that contains the pid, checks to see if the process is running:

> if [ -f $pidfile ]; then
>         $pid=`cat $pidfile`
>         $check=`ps x | grep "^ *$pid"`
>         if [ -z "$check" ]; then
>                 $progname > /dev/null 2>&1
>         fi
> else
>   $progname > /dev/null 2>&1
> fi

-/-

Try:

if [ -f $pidfile ]; then
        pid=`cat $pidfile`
        check=`ps x | grep "^ *$pid"`
        if [ -z "$check" ]; then
                $progname > /dev/null 2>&1
        fi
else
  $progname > /dev/null 2>&1
fi

And: watch out for unset variables ($pidfile, $pid, $progname, ...).

Marcel
--
____   __ . . __

| | | |__  _| | | National Aerospace Laboratory NLR  fax: +31 20 5113210

 
 
 

crontab sh problem

Post by Dan Merc » Wed, 14 Feb 1996 04:00:00


: >
: > I have written a short sh script to check if a process is running and
: > start it if it isn't. The process looks for a file (which may or may not
: > exist) that contains the pid, checks to see if the process is running:
: >
: > if [ -f $pidfile ]; then
: >         $pid=`cat $pidfile`
: >         $check=`ps x | grep "^ *$pid"`
: >         if [ -z "$check" ]; then
: >                 $progname > /dev/null 2>&1
: >         fi
: > else
: >   $progname > /dev/null 2>&1
: > fi
: -/-

: Try:

: if [ -f $pidfile ]; then
:         pid=`cat $pidfile`
:         check=`ps x | grep "^ *$pid"`
:         if [ -z "$check" ]; then
:                 $progname > /dev/null 2>&1
:         fi
: else
:   $progname > /dev/null 2>&1
: fi

: And: watch out for unset variables ($pidfile, $pid, $progname, ...).

: Marcel
: --
: ____   __ . . __

: | | | |__  _| | | National Aerospace Laboratory NLR  fax: +31 20 5113210

kill -0 $pid is much more efficient than ps|grep:

if [ -f $pidfile ]; then
    pid=`cat $pidfile`
    # kill the pid with -0
    # an error indicates that it has died
    kill -0 $pid >/dev/null 2>&1 || $progname > /dev/null 2>&1
else
    $progname > /dev/null 2>&1
fi

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of 3M.

 
 
 

1. crontab sh problem

Indeed!  I think that you also have problems with your ps command.
Straight from the MAN page:

ps prints  information  about  active  processes.   Without
options,  ps  prints  information about processes associated
with the controlling terminal.

Try using some of the options available for ps such as the -e option
which will print out information of all processes currently running.
BTW, what is x ? :-|

should be:
          pid=`cat $pidfile`
          check=`ps -e | grep "^ *$pid"`

Hope this helps!

Lester :-)

--
Lester Claudio                             1175 Chapel Hills Dr.
MCI Communications       \\\\\\//          Colorado Springs, CO 80918

=====================oOOo===()===oOOo================================

2. Sun and HP Training question

3. crontab problem: I can't run a java program via crontab

4. BIOS32 Extensions ??

5. crontab entries generate error message; problem with ownership of crontab file?

6. Runlevel Editor in RedHat 4.2

7. fix to crontab problem ("no changes made to crontab")

8. Shared memory consistency problem (AIX 4.3/SP)

9. Execute a sh script under perl and sh: sh script; perl script?

10. Sh script and crontab - help - thx

11. crontab -e or crontab -l is not functioning properly

12. To crontab or NOT to crontab!!!

13. link a crontab -> "crontab -e" remove link