Send an Alert to Notification Center from the Command Line 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"
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.
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.
Will not work if using launchd.
Why would it have to build as a app bundle, you can’t just keep the executable to run, really inconvenient.
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.
@Scooby
yes working in 10.9.2 here
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.
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
Change the behaviour in System Preferences. Find terminal-notifier listed in the Notifications pref pane, and set the alert style to Alerts, or Banners.
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.
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.
Is there any way to get those notifications on all devices? Maybe sent the message to my icloud account?
How to remove it ??
Click the little (x) icon in the Notification panel to remove alerts
terminal-notifier is only supported on Mac OS X 10.8, or higher.
:(
Right, Notification Center is in OS X 10.8 or later
Growlnotify will allow you to do this with 10.7 or earlier (as well as 10.8).
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.
The terminal command notification sounds like a great idea but can anyone explain how to make it work?
You shouldnt use special characters like ! inside double quotes because of shell expansion
-bash: !: event not found
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.
Yes, it posts a desktop alert in addition to showing up in Notification Center. Updated with a screenshot demonstrating this.