Skip to content

Instantly share code, notes, and snippets.

@arthurafarias
Last active October 17, 2023 22:16
Show Gist options
  • Save arthurafarias/6c882d324b7238bc0b7dded02a9350e2 to your computer and use it in GitHub Desktop.
Save arthurafarias/6c882d324b7238bc0b7dded02a9350e2 to your computer and use it in GitHub Desktop.
A simple script to send an e-mail using ssmtp when a site is up (bash)

Send an e-mail when a server is online

Install

To install this script globally just run the following command:

wget -NP https://gist.githubusercontent.com/arthurafarias/6c882d324b7238bc0b7dded02a9350e2/raw/95a3d9d81a49811fdba9c2d84c925b58a2f7b3b3/check-server-up.sh /usr/local/bin/check-server.sh

Example

check-server.sh -i 1 -c 200 -a target@example.com -u http://google.com -m "Google is Up"
check-server.sh -i 1 -c 500 -a target@example.com -u http://google.com -m "Google is Down"
#!/bin/bash
while getopts u:i:c:a:m: flag
do
case "${flag}" in
u) url=${OPTARG};;
i) interval=${OPTARG};;
c) code=${OPTARG};;
a) email=${OPTARG};;
m) message=${OPTARG};;
esac
done
echo "Starting to check URL: $url";
echo "Lookup interval: $interval";
echo "code: $code";
echo "Email: $email";
echo "Message: $message";
while true; do
result=$(curl -q -L -I $url 2>&1 | grep $code | wc -l)
if [[ $result -gt 0 ]]; then
echo $(date): "Server is up: $url"
msg=$(echo "$(date): $message" | sed -E -e s/:/-/g)
echo $msg | ssmtp $email
exit
else
echo $(date): "Server is down: $url"
fi
sleep $interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment