Last active
August 29, 2015 14:07
-
-
Save mmrwoods/bd5474384c1fe1459588 to your computer and use it in GitHub Desktop.
Quick hack to check apache is running and responding, and start/restart if not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PS_NAME=apache2 | |
CHECK_URL=http://localhost/ | |
MAX_TRIES=5 | |
apache_running() { | |
ps -eo pid,comm | fgrep $PS_NAME > /dev/null 2>&1 | |
} | |
apache_responding() { | |
wget --quiet --spider --timeout=5 $CHECK_URL | |
} | |
if ! apache_running; then | |
echo "Apache server not running, starting!" | |
service apache2 start | |
exit 1 | |
fi | |
try_count=0 | |
while [ $try_count -le $MAX_TRIES ]; do | |
if apache_responding; then | |
exit 0 | |
fi | |
try_count=$[$try_count+1] | |
sleep 5 | |
done | |
echo "Apache failed to respond after $MAX_TRIES attempts, restarting!" | |
service apache2 restart | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment