Growl upon job completion in zsh

Growl is a pretty classy system for notifications among Mac applications; there are similar notifications in other desktop environments. Whenever I run a shell command that is going to take a while to complete, it's natural to switch to something else in the meantime. So I would like a notification as soon as the original command completes.

Growl comes with a script, ‘growlnotify’, that can trigger notifications from the command-line. You could, for example, type something like ‘make; growlnotify done’ – the problem is that I never remember that I would like the notification until after the command is already running.

So here is an alternative: this bit of zsh code will notify me upon completion of any job that takes longer than 5 seconds.

## zsh/growl integration: any command that takes longer than 5 seconds
## will trigger a growl notification when it completes.

if growlnotify -h &>/dev/null; then
    preexec() {
        zsh_growl_time=`date +%s`

    precmd() {
        if (( $? == 0 )); then
        if [[ "${zsh_growl_cmd}" != "" ]]; then
            if (( `date +%s` - ${zsh_growl_time} > 5 )); then
                growlnotify -m ${zsh_growl_cmd} ${zsh_growl_status}

Note that this is specific to zsh, which is not the default shell on OS X, although it is supplied with the system. Possibly it can be made to work with other shells; I really don't care. :)

Ideas for improvements: filter out commands that are obviously going to be long, interactive jobs, such as ssh sessions, vi, command-line emacs, etc. Also, is there some way not to rely on ‘/bin/date’?

©20022015 Christopher League