[ACCEPTED]-bash script to restart Apache automatically-bash
We used to have Apache segfaulting sometimes 5 on a machine; here's the script we used 4 trying to debug the problem while keeping 3 Apache up. It ran from cron (as root) once 2 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, 20050606 PATH=/bin:/usr/bin THEDIR=/tmp/apache-watchdog EMAILfirstname.lastname@example.org mkdir -p $THEDIR if ( wget --timeout=30 -q -P $THEDIR http://localhost/robots.txt ) then # we are up touch ~/.apache-was-up else # 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 fi fi rm -rf $THEDIR
We 1 never did figure out the problem...
Can the count of a process really be less 1 than zero?
This should be sufficient:
if ! pgrep apache2 -c >/dev/null; then
You could try to send an http request to 3 apache (e.g. using
wget --timeout=10) and if that request 2 times out or fails (exit status != 0), you 1 kill and restart apache.
Why would Apache hang? Can you get to the 5 cause?
There are a number of scripts and 4 tools out there to 'daemonize' apps and 3 watch over them. As you seem to be on Debian 2 or Ubuntu, have a look at the packages
daemon and 1
daemontools. I am sure there are others too.
More Related questions