How can I trigger a notification when a job/process ends?

The place I work at has commands that take a long time to execute.

Is there a command/utility that I can use to notify me when the command execution is over? It could be a popup window or maybe a little sound.


Generally, if you know this before running the command, you can just start it with:

command; command-after &

This will execute the command-after after the previous command has exited (regardless of its exit code). The & will start it in background.

If you care about a successful or failure exit, respectively use:

command && command-after-only-if-success &
command || command-after-only-if-fail &

If the command has already started you may use job control to suspend it, then return it to the foreground with fg chained with your notification:

command
# enter Ctrl-z
fg ; command-after

Now … what you want to do after this depends on your environment.

  • On any system, you can "ring" the terminal bell. Depends on your exact system what really works (BSD vs. GNU Linux, etc.), but tput bel should do. I couldn't reliably test it right now, though. Search for "ring bell" to find out more.

  • On Mac OS X, you could use AppleScript to pop up a Finder dialog:

    osascript -e 'tell Application "Finder" to display dialog "Job finished" '
    

    You could have it say something to you:

    say "Job finished"
    

    Or you could use Mountain Lion's notification system:

    sudo gem install terminal-notifier # <= only need to do this once
    terminal-notifier -message "Job finished!" -title "Info"
    
  • In GNOME, zenity can show a GTK dialog box, called from the command line. See also this Stack Overflow question: showing a message box from a bash script in linux. It can be installed through your favorite package manager.

    zenity --info --text="Job finished"
    
  • Some distributions might have xmessage. Specifically for GTK environments, there is gxmessage.

  • On desktop enviroments that implement the Desktop Notifications Specification, such as Ubuntu and GNOME, there's a notification system that you can trigger with notify-send (part of libnotify).

    notify-send "Job finished!"
    
  • KDE uses kdialog, for example:

    kdialog --passivepopup 'Job finished'
    

On unix-like systems you can ring the audible-bell:

echo -e "\a"

I created a simple tool, for MacOS X, that does exactly this. https://github.com/vikfroberg/brb

Installation

$ npm install -g brb

Instructions

$ sleep 3; brb

I wrote ntfy for exactly this purpose. It is cross-platform and can automatically send notifications when long running commands finish.

If you have Python's pip (most Linux distros and MacOS have it), here's how to install it and enable automatic notifications:

$ sudo pip install ntfy
$ echo 'eval "$(ntfy shell-integration)"' >> ~/.bashrc
$ # restart your shell

Check it out at http://ntfy.rtfd.io

In addition to that, it also can:

  • supress automatic notifications when the terminal is in the foreground (X11, iTerm2 & Terminal.app supported and enabled by default)
  • send cloud-based notifications (Pushover, Pushbullet, and XMPP)
  • be used to send notifications when a process ends (not the aforementioned automatic support)
  • manually send notifications (good for use in scripts!)

To get a sound notification you can use spd-say "Some text". Example:

some-command; spd-say "Yo"

UPDATE

If you do not have the speech-dispatcher pre-installed, you can install it via:

sudo apt-get install speech-dispatcher