Inspired by my previous script, I decide to create simple shell script to check and monitor my server and run it on my Mac.

The script uses ping command to check my server, if exit status is not equal to zero, it means my server is down and it will create notification on my Mac.

#!/bin/bash

# How to use: ./check_server.sh example.com

domain=$1

echo "Checking ${domain}"
ping -c 3 -q $domain > /dev/null
exit_status=$?

if [ $exit_status -eq 0 ]; then
  echo "${domain} is up"
fi

echo "Monitoring the status of ${domain} every 1 minute"

while true
do
  # -c <number>: Stop after sending <number> packages
  # -q: quiet output
  ping -c 3 -q $domain > /dev/null
  exit_status=$?

  if [ $exit_status -ne 0 ]; then
    echo "${domain} is down"
    osascript -e 'display notification "check_server.sh" with title "Status" subtitle "Server is down!" sound name "Hero"'
  fi

  # check every 1 minute
  sleep 60
done

You can get my script above on Github.