Simple Website Monitoring

<h3 class="entry-title">Simple Website Monitoring</h3>
Posted on

Over the course of managing my website and applications on VPS' provided by Digital Ocean, I have from time to time encountered moments where my site has inexplicably been down. Rather than finding out my site was down by visiting the site directly, I'd prefer that the process be automated and that I receive a notification on my work computer.

  • Create a job that runs on an interval to check if the website is up
  • Receive a notification in the Ubuntu notification menu if the call to the website returns any code other than 200.

Bash Script

pid=$(pgrep -u $username nautilus)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | tr '\0' '\n' | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )

result=`/usr/bin/curl -s -o /dev/null -w "%{http_code}"`

if [[ $result != '200' ]]; then
     /usr/bin/notify-send -t 0 ' is Down!';

Things I learned

  • The crontab job runs with a different environment than the user. It is advised to pass full paths for binaries.
  • In order for /usr/bin/notify-send to work it is necessary (at least on Ubuntu 19.10) to set the DBUS_SESSION_BUS_ADDRESS environment variable in order for the notification to be displayed.
  • /var/log/cron.log needs to be enabled in /etc/rsyslog.d/50-default.conf (for 19.10 at least)
  • Can also check logs by running grep CRON /var/log/syslog

This will run the script every 5 minutes.

*/5 * * * * /bin/bash /home/travis/local/bin/