Building simplicidade.org: notes, projects, and occasional rants

Tip: use growlnotify for long running Terminal tasks

If you have Growl installed, and you use the Terminal.app a lot, then make sure you also install the growlnotify command line tool.

This will allow you to send Growl notifications from the command line. The most useful script I have, that I use constantly is this:

#!/bin/sh
#
# Runs script, and prints a notification with growl when it finishes
#

$*
growlnotify -m "Script '$*' completed" -s "Background script notification" &

My version is called n, just the single letter n.

This allows me to do:

n scp server:some_big_file .

and a Grown notification will appear when the process terminates.

Update: a couple of updates with great suggestions from Ranger Rick, Tim Bunce and Ruben Fonseca. The new version below should also work on Linux systems, using libnotify. It was updated to deal with arguments containing spaces and we keep the exit status of the command intact. Also we assume exit code 0 is success, all others mean some failure has occurred. Thanks to all.

The Linux version needs checking, I didn't have a Linux server with libnotify, and used Ruben suggestions blindly. Please leave a comment if it doesn't work, specially the detection code. You might need a -h in there.

The full script (download):

#!/bin/sh
#
#  Runs script, and prints a notification with growl when it finishes
#
# Written sometime in 2006, posted 2007/08
#
# With Tips from Ranger Rick, Tim Bunce and Ruben Fonseca
#

# Run the command, including arguments with spaces
"[email protected]"
status=$?

# decide which status to use
if [ "$status" == "0" ] ; then
    result="completed"
else
    result="FAILED ($status)"
fi

# decide which notifier we have
env growlnotify -h > /dev/null 2> /dev/null
has_growl=$?
env notify-send -? > /dev/null 2> /dev/null
has_libnotify=$?

# notify the user, growl or libnotify
if [ "$has_growl" == "0" ] ; then
    growlnotify -m "Script '[email protected]' $result" -s "Background script notification" &
elif [ "$has_libnotify" == "0" ] ; then
    notify-send "Script '[email protected]' $result" "Background script notification" &
fi

# exit with the original status
exit $status

Update 2: typo in "has_libnotify" corrected. BTW, I'm using env to check availability of a command because which (my first choice) returns the same exit code if the program exists or not, at least in Mac OS X.

Update 3: Thanks to Ruben Fonseca, the above script (and the download version) now work correctly on Linux with libnotify.