Send an Alert to Notification Center from the Command Line in OS X

Aug 3, 2012 - 21 Comments

Send Alert to Notification Center in OS X

Using an excellent third party tool called terminal-notifier, you can post alerts and messages to Notification Center directly from the command line. This has a myriad of potentially valid uses, but one fantastic use-case is along the same veins of verbally announcing when a command has completed or sending a badge alert, but instead posting the notification to OS X Mountain Lion’s Notification Center.

Installing Terminal Notifier

Assuming you have ruby on the Mac, you can easily install terminal-notifier using gem:

sudo gem install terminal-notifier

For those without ruby, you can download a pre-built binary from GitHub but to run terminal-notifier you have to point it to the binary inside the app bundle as so:

./terminal-notifier.app/Contents/MacOS/terminal-notifier

If you go the latter route, you’d best off creating an alias in bash_profile. For the purpose of this article we’ll assume you installed it through ruby.

Using Terminal Notifier to Post to Notification Center

Once installed, using the command at it’s most basic core is as follows:

terminal-notifier -message "Hello, this is my message" -title "Message Title"

Notification Center popup alert as posted from the command line

Posting a message after a command has completed is easy, just append terminal-notifier as so:

ping -c 5 yahoo.com && terminal-notifier -message "Finished pinging yahoo" -title "ping"

These post a noninteractive notification, but digging deeper you can launch applications, execute terminal commands, and open URLs too.

Making Notifications Interactive: Opening URL’s, Applications, and Executing Terminal Commands

Even better though are the -open and -activate commands though, which let you either specify a URL or an application to activate when the Notification is clicked. For example, this will open osxdaily.com when clicked:

terminal-notifier -message "Go to OSXDaily.com, it's the best website ever!" -title "osxdaily.com" -open https://osxdaily.com

The notification posts to Notification Center, and if clicked it will open osxdaily.com in the default web browser.

The next example will open TextEdit if you click on the notification:

terminal-notifier -message "Time to braindump into TextEdit" -title "Braindump" -activate com.apple.TextEdit

You can also execute terminal commands if the notification is interacted with:

terminal-notifier -message "Time to run your backups" -title "Backup Script" -execute backupscript

That’s just a few examples, but there are obviously infinite uses for such a thing. Considering how useful this is I’m surprised Apple didn’t include a way to do this into OS X, though that could change some day. In the meantime enjoy terminal-notifier, it’s a great tool.

.

Related articles:

Posted by: William Pearson in Command Line, Mac OS, Tips & Tricks

21 Comments

» Comments RSS Feed

  1. Dan says:

    I would love to notify a user to restart their computer daily and get notified of uptime is they have exceeded some time. How do you pipe the result of a terminal command (like “uptime”) to the notification and only post it if the result is greater than 24 hours, etc? It should only run once every 2 hours or some long term.

  2. ddmytrenko says:

    Will not work if using launchd.

  3. meow says:

    Why would it have to build as a app bundle, you can’t just keep the executable to run, really inconvenient.

    • gp says:

      It’s packaged as an app because NSUserNotification doesn’t work from a Foundation tool, the framework will only let applications send notifications I guess.

  4. Samy says:

    @Scooby
    yes working in 10.9.2 here

  5. ScoobyDoo says:

    Has anyone been able to make this work in 10.9? I installed it, have Notifications enabled in SysPref but I’m not getting any notifications showing from the terminal either with or without sudo.

  6. amin ghadesi says:

    terminal-notifier -message “Go to OSXDaily.com, it’s the best website ever!” -title “osxdaily.com” -open https://osxdaily.com

    chang to:

    terminal-notifier -message “Go to OSXDaily.com, it’s the best website ever\!” -title “osxdaily.com” -open https://osxdaily.com

  7. James says:

    Change the behaviour in System Preferences. Find terminal-notifier listed in the Notifications pref pane, and set the alert style to Alerts, or Banners.

  8. Aaron says:

    Unless I missed it, this doesn’t seem to have a way to make the notification stay on the screen, instead of sliding into the notification center after a couple of seconds.

  9. Aaron says:

    Doing “command && terminal-notify” will only display the notification if the command succeeds (i.e., returns 0). If you want it to show regardless, use ; instead of && (like “command; terminal-notify”). Alternately, if you want to notify only when the command fails, use || (like “command || terminal-notify”). If you want more advanced logic than that, I suggest you read up on how to do if-then clauses in bash scripting.

  10. alain says:

    Is there any way to get those notifications on all devices? Maybe sent the message to my icloud account?

  11. Tapeseries5 says:

    How to remove it ??

  12. terminal-notifier is only supported on Mac OS X 10.8, or higher.

    :(

  13. William Scott says:

    Here is one example:

    Say I want to run some command that takes several minutes, so I want to do something else while it runs but receive notification when it is done. I also hate the terminal bell sound, so I turned it off. I can use this as follows:

    In bash or zsh, I can define a function like this:

    tnot () { “$@” && terminal-notifier -message “$( echo $@ ) returned $?” -title “Command $HISTCMD Completed” >/dev/null ; }

    Now, I can issue

    tnot sleep 12

    It runs the sleep command (waits 12 seconds), and when it has finished, it posts the notification.

    Now I need to figure out how to bind it to something like option-return, so I can invoke it without typing anything.

  14. Daniel says:

    The terminal command notification sounds like a great idea but can anyone explain how to make it work?

  15. Napoleon Wils0n says:

    You shouldnt use special characters like ! inside double quotes because of shell expansion

    -bash: !: event not found

  16. Jason says:

    Does this send a desktop notification as well, or does it only post to the Notification Center panel? Looks great will check it out later.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site