We can create a periodic task that checks our website every 5 minutes and sends a message to our phone number in case something is wrong.
*/5 * * * *
url="http://yourwebsite.com/health"
max_attempts=5
twilio_sid="your_twilio_account_sid"
twilio_from_number="your_twilio_phone_number"
phone_numbers=("number1" "number2" "number3" "number4" "number5")
send_twilio_alert() {
for phone_number in "${phone_numbers[@]}"
do
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$twilio_sid/Messages.json" \
--data-urlencode "Body=Alert: The website $url is not responding!" \
--data-urlencode "From=$twilio_from_number" \
--data-urlencode "To=$phone_number" \
-u "$twilio_sid:$CRED_TWILIO"
done
}
# Exponential notation is used to check if website is up or not
attempt=1
while [ $attempt -le $max_attempts ]
do
response=$(curl -s -o /dev/null -w "%{http_code}" $url)
if [ "$response" -eq 200 ]; then
echo "Website is up!"
exit 0
else
echo "Attempt $attempt failed with response code $response. Waiting before retry..."
sleep $((2**attempt))
fi
attempt=$((attempt+1))
done
echo "Website is down! Sending alert..."
send_twilio_alert