Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmrwoods/bd5474384c1fe1459588 to your computer and use it in GitHub Desktop.
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
#!/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