Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 13 Feb 2005 13:54:03 -0600
From:      "Paul Schmehl" <pauls@utdallas.edu>
To:        =?iso-8859-1?B?S/Z2ZXNk4W4gR+Fib3I=?= <gabor.kovesdan@t-hosting.hu>, <freebsd-questions@freebsd.org>
Subject:   Re: Crontab script
Message-ID:  <042c01c51205$d0429c90$7702a8c0@officeeagle>
References:  <420FA6F5.1020609@t-hosting.hu>

next in thread | previous in thread | raw e-mail | index | archive | help
----- Original Message ----- 
From: "Kövesdán Gábor" <gabor.kovesdan@t-hosting.hu>
To: <freebsd-questions@freebsd.org>
Sent: Sunday, February 13, 2005 1:13 PM
Subject: Crontab script


> Hi,
>
> I've seen somewhere an easy way to check whether a program with a 
> specified pid is running or not. I've made a crontab script to check my 
> programs based on this. The script is the following:
>
> #!/bin/sh
> PID_FILE="/usr/local/bopm/var/bopm.pid"
> PID=`cat $PID_FILE`
> EXECUTABLE="/usr/local/bopm/bin/bopm"
>
Check for a pid file is not a good way to see if a program is running. 
*Sometimes* they will be running even though there is no pid file (even 
though there's supposed to be one.)

This would be bettter:

either ps -auxw | grep {program name} | awk {'print $2'}
or pgrep {program name}  (pgrep is available on the web)

If you chose the former, you may have to put in a second grep to eliminate 
"finding" your own command.  Something like this:
ps -auxw | grep {program name} | grep {commandline switch of the program} | 
awk {'print $2'}

You will want to test this on the commandline first to make sure you're 
getting the right process.

Putting this all together then, with a specific example that I know about:

#!/bin/sh

APACHE=/usr/local/sbin/apachectl
PID=`ps -auxw | grep httpd | grep "\-DSSL" | grep root | awk {'print $2'}`
DATE=`date +"%m-%d-%H:%M:%S"
LOG=`tail /var/log/httpd-error.log`

if  [ ! -z $PID ]; then
  $APACHE start
  echo "Restarted apache at $DATE"
  echo $LOG
fi

This will check to see if it's running, and if it's not, start it and send 
you the date/time it was started and the last 10 lines of the error log. 
Since you're running it in cron, you'll get email with the output.  If you 
wanted, you could redirect stderr to a log to see if there were any 
problems.

Paul Schmehl (pauls@utdallas.edu)
Adjunct Information Security Officer
The University of Texas at Dallas
AVIEN Founding Member
http://www.utdallas.edu/~pauls/ 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?042c01c51205$d0429c90$7702a8c0>