Skip to content

Instantly share code, notes, and snippets.

@sjmcgrath
Created April 27, 2017 00:03
Show Gist options
  • Save sjmcgrath/83730cc947c87fca6a764c53fec9b741 to your computer and use it in GitHub Desktop.
Save sjmcgrath/83730cc947c87fca6a764c53fec9b741 to your computer and use it in GitHub Desktop.
Checks a URL for a response with retries and wait periods.
#!/bin/bash
NUMBER_OF_RETRIES={NUMBER_OF_RETRIES:-5}
WAIT_IN_SECONDS=${WAIT_IN_SECONDS:-30}
URL="${1:-127.0.0.1/index.php}"
URL_HASH="$( echo -n "${URL}" | cksum | sed 's/\s//g' )"
LOCK_FILE="/tmp/.page_ok.${URL_HASH}"
(
flock --nonblock 9 || exit 0
wget --output-document=- --retry-connrefused --tries=${NUMBER_OF_RETRIES} --wait=${WAIT_IN_SECONDS} --quiet "${URL}" > /dev/null
) 9>"${LOCK_FILE}"
@sjmcgrath
Copy link
Author

Useful for monitoring and restarting a service via crontab. (But don't do that, there are better options with Upstart and systemd.)

If you do though, you can do something like this in your crontab...

* * * * * page_ok 127.0.0.1/index.php || restart-web-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment