Last active
March 31, 2023 08:51
-
-
Save sim51/266b017e978d663c9c69c2a9d4fa539f to your computer and use it in GitHub Desktop.
Bash script that runs a command when a website is done
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 | |
set -e | |
# | |
# Script variables | |
# | |
# The url to check | |
WEBPAGE="https://www.bsimard.com/" | |
# Timeout | |
MAX_TIME=5 | |
# Command to exec when site is down | |
COMMAND="sudo systemctrl nginx restart" | |
# Check if the script is already running | |
# If the command takes a long time, we don't want to execute it again | |
FILENAME=$(basename $0) | |
for pid in $(pidof -x ${FILENAME}); do | |
if [ $pid != $$ ]; then | |
echo "[$(date)] : Process is already running with PID $pid" | |
exit 1 | |
fi | |
done | |
# Do a call to the url, and if its response is not a 200 execute the script | |
HTTPCODE=$(curl --fail --max-time ${MAX_TIME} --silent --write-out %{response_code} --output /dev/null "$WEBPAGE" | head -n 1) | |
if [[ $HTTPCODE -ne 200 ]]; then | |
echo "[$(date)] : Website ${WEBPAGE} is down, executing ${COMMAND}" | |
eval "${COMMAND}" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment