Crontab script

Paul Schmehl pauls at utdallas.edu
Sun Feb 13 19:54:24 GMT 2005


----- Original Message ----- 
From: "Kövesdán Gábor" <gabor.kovesdan at t-hosting.hu>
To: <freebsd-questions at 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 at utdallas.edu)
Adjunct Information Security Officer
The University of Texas at Dallas
AVIEN Founding Member
http://www.utdallas.edu/~pauls/ 



More information about the freebsd-questions mailing list