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

91,922

Solution 1

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'
    

Solution 2

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

echo -e "\a"

Solution 3

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

Solution 4

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!)

Solution 5

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
Share:
91,922

Related videos on Youtube

Utkarsh Sinha
Author by

Utkarsh Sinha

Works with several languages and technologies to create new things.

Updated on September 18, 2022

Comments

  • Utkarsh Sinha
    Utkarsh Sinha over 1 year

    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.

    • Krazy Glew
      Krazy Glew over 7 years
      @slhck's accepted answer is cool - I should have realized that fg;say "Job finished" would work. But... is there any way that it can be further automated - i.e. ring the bell or notify after completion of any job that takes more than a threshold like a minute? Is there a shell variable, e.g. in bash, that is elapsed time of the last command?
    • Krazy Glew
      Krazy Glew over 7 years
      ... 2 hours later, found stackoverflow.com/questions/1862510/… ... put '(( $timer_show > ${LONG_RUNTIME:-300} )) && say "long running job completed"' in timer_stop ... next, add to emacs' compile commands ... and notify my Pebble watch (I hate phone notifications)
  • Utkarsh Sinha
    Utkarsh Sinha over 12 years
    I'm fiddling around with notify-send and xmessage. Both of them seem to be interesting! Here's the next thing I'm looking for - superuser.com/questions/345463/…
  • Utkarsh Sinha
    Utkarsh Sinha over 12 years
    I'm trying to avoid looking at the shell to find out if a job finished. Thanks, though!
  • trinth
    trinth over 11 years
    on Mac OS X, you can also use the command-line utility "say". there are also many voices available, check "say -h" ;)
  • Allison
    Allison about 11 years
    This is really useful in addition to a message notification on mac
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    Why are you suggesting that "command-after" should be run asynchronously?  Did you mean to say (command; command-after) &?
  • Dwight Spencer
    Dwight Spencer over 8 years
    same as tput bel
  • David Richerby
    David Richerby over 8 years
    That's just a verbose way of echoing \a (trinth's answer).
  • MrMas
    MrMas over 8 years
    This solution is for Konsole users and isn't a perfect solution as it relies on your command either being verbose until it is complete, or completely silent (no output) until it completes at which point the prompt returns. However, this is exactly what I needed when I came looking for help. You can configure Konsole to pop-up a notification, play a sound, etc. Then you have to turn on shell monitoring. Monitor for silence if your command outputs a bunch of stuff until it completes, or silence if it doesn't print out anything and then the shell returns.
  • MrMas
    MrMas over 8 years
    Also note that you can configure different responses for different things: silence, activity, non-zero return value, Just click Settings->Configure Notifications...and you can pop up a notification, play a custom sound, log a file, run a command -- pretty versatile -- though I wish there were more actions -- included session finished (regardless of exit status)
  • pythonian29033
    pythonian29033 almost 8 years
    Sweet! added to my .bashrc as an alias :D
  • Edward Falk
    Edward Falk almost 8 years
    What OS? I don't see it on Mac or Ubuntu
  • Edward Falk
    Edward Falk almost 8 years
    OK, so "pushover" is a web service that sends notifications to phones that have the "pushover" app installed?
  • milkovsky
    milkovsky almost 8 years
    @EdwardFalk I am using Ubuntu
  • Edward Falk
    Edward Falk almost 8 years
    OK, I'm using a pretty old version, so maybe it's in newer versions.
  • milkovsky
    milkovsky almost 8 years
    It is just a text-to-speech application. You can install it via sudo apt-get install speech-dispatcher. Or use alternatives askubuntu.com/questions/501910/…
  • Edward Falk
    Edward Falk almost 8 years
    Ahh, not installed by default? I should have thought about that. Thanks for the link.
  • Ciro Santilli Путлер Капут 六四事
    Ciro Santilli Путлер Капут 六四事 almost 8 years
    @milkovsky at least on Ubuntu 14.04 it is pre-installed: releases.ubuntu.com/trusty/…
  • Thatkookooguy
    Thatkookooguy almost 8 years
    it's a web service that sends notifications to phones, computers, & everything with a modern web browser. So, you can have pushover open on your PC and phone and be notified on all devices. neat, huh? :-)
  • Adam Bittlingmayer
    Adam Bittlingmayer over 6 years
    Good on you. This is really the most usable answer.
  • Michael
    Michael over 6 years
    I agree. I use it to display a notification on my desktop and push the notification to my phone at the same time.
  • SilverCorvus
    SilverCorvus almost 5 years
    notify-send is part of libnotify and is relevant to Gnome and any other desktop enviroment that implements the desktop notification standard
  • slhck
    slhck almost 5 years
    @CurlyCorvus I'm sorry your edit got rejected; it was good. I applied it to the post. Thanks for the update.
  • Noumenon
    Noumenon about 4 years
    Since 2018 the zenity answer gives a "This is discouraged" warning, which can be suppressed using askubuntu.com/a/1192137/435408.