(Some of this may also apply to OS X, since it’s Unix- like.)
Edit: The general idea does translate well to OS X, but there are a few key differences which Maymay has noted in the comments.
I was digging around in my home directory today and came across a script I’d written last May during one of Github’s outages to alert me when it came back up. Yesterday I turned out a one-liner to watch a website and alert me when it changed, so I was struck by how my problem-solving approach had changed in the intervening months as I continued to learn more about the utilities available on the Unix command line.
First, the script from last May:
#!/bin/bash headers=`curl -G https://github.com/ -I -s | grep HTTP` if [[ $headers == *200* ]] then `paplay ~/Downloads/echo_ping.ogg` fi
Essentially this script just grabs the response headers from https://github.com/ and looks for the HTTP code. Then it looks for the string “200” anywhere within the HTTP code string. If a “200” code is returned, it uses paplay to play a sound file I’d downloaded from the command line.
A few things stick out to me here:
If I were to rewrite this today, it’d probably look like this:
watch -n 300 -g curl -s -I https://github.com/ && espeak "Github is up." && xmessage "Github is up."
Here I’m telling “watch” to check the output of “curl -s -I https://github.com/” every 300 seconds (-n 300) and then exit when it changes (-g). Upon exit, “espeak” will make my computer tell me verbally that Github is back, and then once that’s done, “xmessage” will pop up a message box saying the same.
The simplicity is great, and it’s always nice to see evidence of one’s growing proficiency with a tool, but “xmessage” really makes this vastly superior to my old script. The message box it produces is ugly as anything, but it’s much better-suited to the purpose than “notify-send” is. Sometimes you want a notice to hang around until acknowledged, and “xmessage” fits the bill nicely.