How to invoke alerts from the OS X Terminal

If needed, you can have the Terminal alert you when a process or script is complete.

When you run a script or command in the OS X terminal, it can be useful to have some indication for when it is completed, especially if the script is a lengthy process you might not wish to monitor at all times. Unfortunately when a script or command finishes running, the Terminal will drop you to the command line again, without any notice to you. However, if needed, you can set up the system to run commands so it does offer some notification.

System alerts
The first option is to have the terminal invoke the system alert after the command is finished. The system alert sound can be invoked from the Terminal by running "tput bel" at the command line, which can be put after a script or other command in a one-line command sequence similar to the following:

top; tput bel

In this sequence, the first command is run (in this case "top" to view system process status), and when it completes, the command after the semicolon will run. In this case, the terminal will have the system alert sound play.

Speak a phrase
The second option is similar to the first, but uses a different audible alert. OS X provides an integrated text-to-speech technology that can be invoked from the command line using the "say" command, so for instance to have the system speak some text, simply run the following command in the Terminal:

say "Your script is done"

Using this command in a similar manner as the "tput bel" option above, you can have the system speak a custom phrase when the terminal command or other process you are running is completed.

Invoke notifications
Unfortunately the above options will only run once. While you can put the audible alert command along with the terminal's "sleep" command in a script that loops them indefinitely and then invoke this script in order to maintain the alert indefinitely, there is another cleaner option available that makes use of Mountain Lion's notification system.

  1. Download terminal-notifier and place the program in your Utilities folder.
  2. Create a terminal alias to the program's executable by running the command "pico .bashrc" and when the editor opens add the following line to it:

    alias terminal-notifier=/Applications/Utilities/terminal-notifier.app/Contents/MacOS/terminal-notifier

    NOTE: while the alias "terminal-notifier" is used here, you can use any alias string you would like

When this is complete, you will be able to run the terminal-notifier utility from the command line. There are some complex options you can use for customizing notifications (which you can see by running "terminal-notifier" by itself in the Terminal), or you can invoke a basic notification by running the following:

terminal-notifier -message "Script Complete"

With this command now available, you can similarly invoke it sequentially after you run a lengthy script or command, and thereby have the system output a custom alert notification when done. For example, the following command will run the "top" process viewer program, and when the program is quit the system will issue a notification that reads "Top Completed."

top; terminal-notifier -message "Top Completed"

The terminal-notifier utility is required for invoking Mountain Lion's built-in notifications; however, if you do not have Mountain Lion or prefer not to use Notification Center, then you can alternatively install the popular notification system "Growl" and use the "growlnotify" command to perform a similar option. First install Growl and then install GrowlNotify, and then you can run a basic notification using the following terminal command:

growlnotify -m "Script Complete"



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    HOT ON CNET

    How well do you know your surge protector?

    Whether you're looking to add more outlets, or want to add a layer of protection between your gear and the outside world, here's what you need to know.