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.

  • 1
    @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 Oct 26 '16 at 20:10
  • ... 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) – Krazy Glew Oct 26 '16 at 22:59

14 Answers 14

up vote 114 down vote accepted

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.

  • Ubuntu has a notification system that you can trigger with notify-send.

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

    kdialog --passivepopup 'Job finished'
    
  • Let me know what command you used! I added some other options as well. – slhck Oct 11 '11 at 20:54
  • 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 Oct 11 '11 at 21:04
  • 2
    on Mac OS X, you can also use the command-line utility "say". there are also many voices available, check "say -h" ;) – trinth Oct 10 '12 at 18:28
  • 1
    Why are you suggesting that "command-after" should be run asynchronously?  Did you mean to say (command; command-after) &? – G-Man Jun 24 '15 at 3:13
  • 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 Nov 4 '15 at 22:56

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

echo -e "\a"
  • This is really useful in addition to a message notification on mac – Sirens Apr 1 '13 at 18:49
  • 1
    same as tput bel – Dwight Spencer Sep 2 '15 at 18:31

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 have just begun using notifu for desktop notifications from Cygwin. It's a command line notification app for Windows.

Example:

ls -l && /c/notifu-1.6/notifu64 /m "This is a simple Notifu message."

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

some-command; spd-say "Yo"
  • What OS? I don't see it on Mac or Ubuntu – Edward Falk Jul 24 '16 at 20:07
  • @EdwardFalk I am using Ubuntu – milkovsky Jul 25 '16 at 10:40
  • OK, I'm using a pretty old version, so maybe it's in newer versions. – Edward Falk Jul 25 '16 at 17:04
  • 1
    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/… – milkovsky Jul 26 '16 at 7:48
  • Ahh, not installed by default? I should have thought about that. Thanks for the link. – Edward Falk Jul 26 '16 at 16:54

For Linux, there is a nifty trick to do this automatically without having to type a command for notification every time.

First install autokey. This helps defining actions for different keystrokes.

sudo apt-get install autokey-gtk

Now, define a new phrase in autokey and assign the hotkey as Alt+Enter. Add this phrase:

; notify-send "Job finished!" &
  #

Note that a new line after the first line is important.

Also, add a window filter. I use guake and terminal. Include whatever other terminal you use.

.*(guake)|(Terminal).*

You're done!!

Now, everytime whenever you need to get notifications for a command, run it using Alt+Enter instead of Enter/Return.

Source: http://dotpad.blogspot.in/2014/12/notification-when-command-finished.html

Although other answers already covered most of the ways to get notifications on a finished job, I want to give my two cents since you asked your question in the following format:

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

I have the same problem. Sometimes something can run for 15 minutes.

I have the following function in my .bashrc:

# push a notification to your phone. can be handy if you're running a
# build and you want to be notified when it's finished.
push() {
    curl -s -F "token=PUSHOVER_TOKEN" \
    -F "user=PUSHOVER_USER" \
    -F "title=terminal" \
    -F "message=$1" https://api.pushover.net/1/messages.json > /dev/null 2>&1
}

This uses the Pushover app in order to push a notification to my phone. This way I can go to lunch or enter a meeting and still get notified on jobs I started on my computer before I left.

I use it in the following manner:

command_to_run && push "yes! command finished successfully!" || push "awww man! something failed :-("

So, if the command returns a correct exit code, the first push will be executed. On an error, the second one will be executed.

Ofc you need to create a user at Pushover, and register an app to send notifications from https://pushover.net/

hope this helped!

  • OK, so "pushover" is a web service that sends notifications to phones that have the "pushover" app installed? – Edward Falk Jul 24 '16 at 20:09
  • 1
    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? :-) – Thatkookooguy Jul 31 '16 at 7:53

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!)
  • Good on you. This is really the most usable answer. – Adam Bittlingmayer Aug 18 '17 at 9:43
  • I agree. I use it to display a notification on my desktop and push the notification to my phone at the same time. – Michael Dec 7 '17 at 20:30

If you use csh or tcsh as your interactive shell, you can use the notify command:

% long-running-command &
[1] 14431
% notify %1
% 
(later, when the command finishes)
[1]    Done                          long-running-command

You can achieve a similar effect in bash with set -b or set -o notify.

This probably doesn't meet your requirements, since all it does is print a message; I dont' think it can be configured to pop up a window or ring the bell.

  • I'm trying to avoid looking at the shell to find out if a job finished. Thanks, though! – Utkarsh Sinha Oct 11 '11 at 22:20

On systems with 'awk' try

awk 'BEGIN{ print "\a" }'

was the only one to work for me.

If you're using npm then node-notifier provide a cross-platform solution.

notify -t "Agent Coulson" --icon https://raw.githubusercontent.com/mikaelbr/node-notifier/master/example/coulson.jpg -m "Well, that's new. "

Preview

  • Linux KDE

Linux KDE

  • Windows

Windows

  • Mac OS

mac

  • Growl

Growl

Wish I'd noticed this thread years ago. My solution was essentially the same as slhck's, but I wrote a script. I use it all the time. Posting here to share it.

#!/bin/bash

msg='all done'
quiet=false
if [ "$1" = '-q' ]; then quiet=true; shift; fi
if [ $# -gt 0 ]; then msg="$*"; fi

echo -ne "\x1b]0;$msg\a"
if [ -x /usr/bin/zenity ]; then
  unset WINDOWID
  exec zenity --info --text="$msg"
elif [ -x /usr/bin/xmessage ]; then
  exec xmessage -nearmouse "$msg"
elif [ -x /usr/bin/osascript ]; then
  if ! $quiet; then say "done" &; fi
  osascript -e "tell application \"System Events\" to display dialog \"$msg\""
else
  echo $msg
fi

One small bit of explanation: the string "\x1b]0;$msg\a" is the ANSI escape sequence to put the message in the title bar of the window from which it was executed. I find it quite handy sometimes to be able to see which window it was that the message came from.

So this comes fairly late, but I've started using a system to do this: I have a bash script which executes whatever command is passed to it afterwards

http://somesh.io/2017/02/19/get-notified-when-long-commands-are-done-executing-on-ubuntu/

#!/bin/bash


# file location : /usr/bin/n

set +e


# $@ executes whatever command is typed after the filename

$@


notify-send "Task Completed"

and then i simply prepend n

n bundle install
n npm install

Another possibility is to use alert, Works on Linux.

>any-command; alert

It gives a notification as in the image. alert notification

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.