#!/bin/bash echo "This script will create a crontab script and process check" echo "script which will use a pid file to ensure that a process" echo "is still running." echo "Enter the working directory. We will cd here if needed to run the binary: " mypwd=`pwd` echo "Working Directory [$mypwd]:" read workingdir; if [ "$workingdir" = "" ]; then workingdir="$mypwd" fi echo "Enter the command to run:" read bin if [ "$bin" = "" ]; then echo "You must type in a command to run." exit 1 fi echo "Enter the pid file [$mypwd/$bin.pid]:" read pidfile if [ "$pidfile" = "" ]; then pidfile="$mypwd/$bin.pid" fi echo "Give me an output file for this script to save to:" read outputfile if [ "$outputfile" = "" ]; then echo "No output file specified. Exiting." exit 3 fi echo "#!/bin/bash" > $outputfile echo "#" >> $outputfile echo "# This is a script suitable for use in a crontab. It checks to make sure" >> $outputfile echo "# your process is running. YOU NEED A SEPARATE CRON JOB FOR EACH PROCESS." >> $outputfile echo "# If the procdess isn't found, it'll try to start it back up." >> $outputfile echo "#" >> $outputfile echo "# You'll need to edit this script for your process." >> $outputfile echo "#" >> $outputfile echo "# To check every 10 minutes, put the following line in your" >> $outputfile echo "# crontab:" >> $outputfile echo "# 0,10,20,30,40,50 * * * * $mypwd/$outputfile" >> $outputfile echo "# And if you don't want to get email from crontab when it checks your process," >> $outputfile echo "# put the following in your crontab:" >> $outputfile echo "# 0,10,20,30,40,50 * * * * $mypwd/$outputfile >/dev/null 2>&1" >> $outputfile echo "#" >> $outputfile echo "workingdir=\"$workingdir\"" >> $outputfile echo "bin=\"$bin\"" >> $outputfile echo "pidfile=\"$pidfile\"" >> $outputfile echo "if test -r \$pidfile; then" >> $outputfile echo " # there is a pid file -- is it current?" >> $outputfile echo " pid=\`cat \$pidfile\`" >> $outputfile echo " if \`kill -CHLD \$pid >/dev/null 2>&1\`; then" >> $outputfile echo " # it's still going" >> $outputfile echo " # back out quietly" >> $outputfile echo " exit 0" >> $outputfile echo " fi" >> $outputfile echo " echo \"Crontab notice:\"" >> $outputfile echo " echo \"\"" >> $outputfile echo " echo \"Stale \$pidfile file (erasing it)\"" >> $outputfile echo " rm -f \$pidfile" >> $outputfile echo "fi" >> $outputfile echo "echo \"\"" >> $outputfile echo "echo \"Couldn't find \$bin running. Reloading it...\"" >> $outputfile echo "echo \"\"" >> $outputfile echo "cd \$workingdir" >> $outputfile echo "./\$bin" >> $outputfile chmod +x $outputfile