12

(OSX Specific)

I, like many others besides myself often find myself kicking off a process in the shell that takes a minute or two to complete (a large svn commit for example). During that time, I often alt-tab and refresh reddit/slashdot/wikipedia/whatever. It would be great to have something set up that posts a growl notification when the shell process is over.

In my ideal world, it would work like this: "If a process just exited from a tab open in Terminal, post a growl notification."

Anyone else have something like this set up?

2
  • 1
    Note that growl will only notify you if you are still sitting at the screen. For longer programs, use the "say" command to get an audio hint that it is finished.
    – user31752
    Nov 2, 2010 at 19:55
  • @mankoff: You can configure the growlnotify application notification settings in Growl.prefPane to include an audio notification sound. It's not, however, changeable depending on the command you execute, like say would be. But it's way less creepy.
    – Daniel Beck
    Nov 2, 2010 at 20:04

8 Answers 8

11

You can install growlnotify to do this.

$ ./some_program && growlnotify Title -m Message

Of course you would need to think of this before performing your command. The alternative (I don't know how to achieve this though) would be a Growl notification for every single command, which would be insanely annoying.


To simplify use of growlnotify for your use case, edit ~/bash_profile and add the following:

function long {
    $@
    /usr/local/bin/growlnotify Finished -m 'Done'
}

now you can simply long your_command (similar to sudo). Ctrl-A positions the cursor at the beginning of the line, if you (like me) always type the actual command first and need to add the prefix afterwards.

My bash-fu is unfortunately insufficient to be able to add the command to the growlnotify message


per @mankoff's comment to this answer:

You can simply type while the command is running, it gets executed afterwards. I created the following function for me:

function gn {
    /usr/local/bin/growlnotify Finished -m "$@"
}

Use as gn svn.

10
  • 2
    You don't need to think of it before the command. If, while it is running, you realize you want to be notified when it is done, just type "gn foo" (or "say done", for audio hints), press enter, and that command will run when the current one finishes.
    – user31752
    Nov 2, 2010 at 19:54
  • Re you comment on doing this automagically... Use PS1 or something to touch a timestamp. Use PS2 to verify how long since PS1 touched it. If it has been > n seconds, then run growlnotify. Now it isn't annoying because it doesn't happen for every command.
    – user31752
    Nov 2, 2010 at 19:56
  • @mankoff True. So a simple function to reduce the typing is sufficient.
    – Daniel Beck
    Nov 2, 2010 at 19:57
  • @mankoff PS2? Isn't this just the continuation prompt? It doesn't prefix every output line..?
    – Daniel Beck
    Nov 2, 2010 at 20:02
  • The PS1/PS2 were just guesses as places I'd start looking if I wanted to implement this. Perhaps the whole thing can be embedded in just PS1. And perhaps there are entirely different ways to make a generic growl-for-every-command setup.
    – user31752
    Nov 2, 2010 at 21:17
2

What you need is growlnotify which is a CLI program to trigger growl notifications. You could use it as:

./script.sh;; growlnotify -m "done"

It has a few more knobs you can play with, use growlnotify -h for more info

2
  • Thanks. I just commented at the same time when I found that too. Semicolon makes more sense than the && as well.
    – ezrock
    Nov 2, 2010 at 19:01
  • 1
    @ezrock I figure one could use && and || to have different messages between success and failure states, but if one's only interested in a message being shown after the command executes, then ;; is foolproof. =)
    – roguesys
    Nov 2, 2010 at 19:40
2

Modifiying Daniel Beck's answer if you do this slight change it makes it very nice.

function long {
    command=$@
    $command
    /usr/local/bin/growlnotify Finished -m "$command completed."
}

Then when you run 'long svn up' you will see message 'svn up completed.'

Very nice if you run many time consuming shell scripts at the same time.

2

Now that OSX 10.8 and later has native notifications, this is how I accomplish the same goal via terminal-notifier:

  1. Install brew
  2. $ brew install terminal-notifier
  3. Add this to ~/.bash_profile:

    function gn {
        terminal-notifier  -message ' ' -title 'Finished'
    }
    

Make sure that you have sourced your profile with . ~/.bash_profile (not necessary for future terminal sessions), since your profile is sourced automatically when creating a session.

Test with your favorite command, followed by the function call.

ls ; gn

The arguments to terminal-notifier can stand some improving, but a quick attempt of grabbing the previous command to pass to the function wasn't successful and this gets me all of the functionality that I need. Namely, adding ;gn to get a notification at the end of any long-running process.

1

Use growlnotify.

ls && growlnotify -H localhost -m "message"
1

This is a way to have it invoked automatically for "long running" processes: http://hints.macworld.com/article.php?story=20071009124425468

It hooks into the $PROMPT_COMMAND and notifies completion of processes taking longer than 10 seconds to complete. You may need to tweak it to suit your own environment and workflow (for example, ignoring completion of less or editor invocation), but it seems to work reasonably well out of the box.

1
  • I haven't tried it myself, but the completely automated approach sounds really appealing. All of the other solutions here require knowing beforehand that the command will be long-running and remembering to trigger the script (ie. ls ; growlnotify). I like the idea of not needing to manually trigger it.
    – ezrock
    Aug 5, 2014 at 12:02
0

I just poked around some more and found a simple method for this. It's a little more manual than I would like it to be, but here goes.

  1. Install growlnotify (It's in the extras folder of Growl's installation download)1.
  2. Append <&& growlnotify -m "Message"> to a long process.

That works for me right now, but I could do better, methinks.

0

OSX 10.9+ solution with no third-party software:

In OSX 10.9 and above, you can use the AppleScript command display notification to display a growl from the terminal like so:

osascript -e 'display notification with title "Process Completed."'

If you don't want to type all that every time, you can use an alias:

alias growl='osascript -e "display notification with title \"Process Completed.\""'

Example:

long-running-script.sh; growl

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .