How to get notifications from your terminal

by poolik

Last weekend, when I was on the bus trying to push my code over a flaky 3G connection, I started searching for a way to get a notification when my commands in terminal finished. It’s just frustrating to start a push, then continue working on something else, only to discover half an hour later that the push failed because of some network error.

Luckily I found this cool project called terminal-notifier. It’s a command-line tool to send Mac OS X User Notifications. Using it is simple, just add it to your path and type:

Immediately I got the idea to create shell aliases in my ~/.bash_profile and use it with the shell operators && and || to get notifications whether my command is successful or not. I ended up with the following intermediate result:

Though this works rather well, I found it quite tedious to always type the (command && ) || construct. Luckily a much better option is available via shell functions. Here’s what I actually have in my ~/.bash_profile:

Firstly I’ve added an -activate com.googlecode.iterm2 option, which enhances the notifications to open iterm2, when I click on it.

Secondly I turned my aliases into functions and created two new – wn (short for with notification) and n (short for simply notification). This approach is superior, not only because I can simply prepend any command with two letters (wn) and I’ve transformed it to fire notifications, but I get the actual command as a parameter, which I can then display in the notification.

So whenever I want to push my code or transform some videos or download something via wget, I prepend the command with wn, hit enter and blissfully continue with something else until I see a notification like this:

Finder navigation shortcuts

PS! I recommend that you change the notification type from ‘banner’ to ‘alert’ for terminal-notifier, otherwise you can miss the notification.