bash script to restart Apache automatically
We used to have Apache segfaulting sometimes on a machine; here's the script we used trying to debug the problem while keeping Apache up. It ran from cron (as root) once every minute or so. It should be self-explanatory.
#!/bin/sh# Script that checks whether apache is still up, and if not:# - e-mail the last bit of log files# - kick some life back into it# -- Thomas, 20050606PATH=/bin:/usr/binTHEDIR=/tmp/apache-watchdogEMAIL=yourself@example.commkdir -p $THEDIRif ( wget --timeout=30 -q -P $THEDIR http://localhost/robots.txt )then # we are up touch ~/.apache-was-upelse # down! but if it was down already, don't keep spamming if [[ -f ~/.apache-was-up ]] then # write a nice e-mail echo -n "apache crashed at " > $THEDIR/mail date >> $THEDIR/mail echo >> $THEDIR/mail echo "Access log:" >> $THEDIR/mail tail -n 30 /var/log/apache2_access/current >> $THEDIR/mail echo >> $THEDIR/mail echo "Error log:" >> $THEDIR/mail tail -n 30 /var/log/apache2_error/current >> $THEDIR/mail echo >> $THEDIR/mail # kick apache echo "Now kicking apache..." >> $THEDIR/mail /etc/init.d/apache2 stop >> $THEDIR/mail 2>&1 killall -9 apache2 >> $THEDIR/mail 2>&1 /etc/init.d/apache2 start >> $THEDIR/mail 2>&1 # send the mail echo >> $THEDIR/mail echo "Good luck troubleshooting!" >> $THEDIR/mail mail -s "apache-watchdog: apache crashed" $EMAIL < $THEDIR/mail rm ~/.apache-was-up fifirm -rf $THEDIR
We never did figure out the problem...