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

87

49

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.

Utkarsh Sinha

Posted 2011-10-11T20:22:56.623

Reputation: 1 327

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 – 2016-10-26T20:10:38.667

... 2 hours later, found http://stackoverflow.com/questions/1862510/how-can-the-last-commands-wall-time-be-put-in-the-bash-prompt/1862762#1862762 ... 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 – 2016-10-26T22:59:23.580

Answers

122

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'
    

slhck

Posted 2011-10-11T20:22:56.623

Reputation: 182 472

1Why are you suggesting that "command-after" should be run asynchronously?  Did you mean to say (command; command-after) &? – G-Man Says 'Reinstate Monica' – 2015-06-24T03:13:49.650

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 – 2015-11-04T22:56:10.630

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) – MrMas – 2015-11-04T22:59:27.230

Sweet! added to my .bashrc as an alias :D – pythonian29033 – 2016-07-14T10:40:55.053

notify-send is part of libnotify and is relevant to Gnome and any other desktop enviroment that implements the desktop notification standard – CurlyCorvus – 2019-07-27T18:37:28.337

@CurlyCorvus I'm sorry your edit got rejected; it was good. I applied it to the post. Thanks for the update. – slhck – 2019-07-28T09:43:46.207

Let me know what command you used! I added some other options as well. – slhck – 2011-10-11T20:54:02.430

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 - http://superuser.com/questions/345463/getting-the-name-of-the-previous-process

– Utkarsh Sinha – 2011-10-11T21:04:35.027

2on Mac OS X, you can also use the command-line utility "say". there are also many voices available, check "say -h" ;) – trinth – 2012-10-10T18:28:14.277

20

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

echo -e "\a"

trinth

Posted 2011-10-11T20:22:56.623

Reputation: 298

2same as tput bel – Dwight Spencer – 2015-09-02T18:31:50.163

This is really useful in addition to a message notification on mac – Sirens – 2013-04-01T18:49:04.787

8

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

Viktor Fröberg

Posted 2011-10-11T20:22:56.623

Reputation: 81

5

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

some-command; spd-say "Yo"

milkovsky

Posted 2011-10-11T20:22:56.623

Reputation: 201

What OS? I don't see it on Mac or Ubuntu – Edward Falk – 2016-07-24T20:07:07.063

@EdwardFalk I am using Ubuntu – milkovsky – 2016-07-25T10:40:05.910

OK, I'm using a pretty old version, so maybe it's in newer versions. – Edward Falk – 2016-07-25T17:04:22.163

1

It is just a text-to-speech application. You can install it via sudo apt-get install speech-dispatcher. Or use alternatives http://askubuntu.com/questions/501910/how-to-text-to-speech-output-using-command-line

– milkovsky – 2016-07-26T07:48:08.107

Ahh, not installed by default? I should have thought about that. Thanks for the link. – Edward Falk – 2016-07-26T16:54:48.703

@milkovsky at least on Ubuntu 14.04 it is pre-installed: http://releases.ubuntu.com/trusty/ubuntu-14.04.4-desktop-amd64.manifest

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2016-07-28T11:51:15.887

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

Daniel Schep

Posted 2011-10-11T20:22:56.623

Reputation: 41

Good on you. This is really the most usable answer. – Adam Bittlingmayer – 2017-08-18T09:43:04.917

I agree. I use it to display a notification on my desktop and push the notification to my phone at the same time. – Michael – 2017-12-07T20:30:56.070

4

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."

Ashutosh Jindal

Posted 2011-10-11T20:22:56.623

Reputation: 322

3

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

shivams

Posted 2011-10-11T20:22:56.623

Reputation: 1 269

3

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!

Thatkookooguy

Posted 2011-10-11T20:22:56.623

Reputation: 141

OK, so "pushover" is a web service that sends notifications to phones that have the "pushover" app installed? – Edward Falk – 2016-07-24T20:09:47.093

1it'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 – 2016-07-31T07:53:41.650

1

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

Édouard Lopez

Posted 2011-10-11T20:22:56.623

Reputation: 220

1

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.

Edward Falk

Posted 2011-10-11T20:22:56.623

Reputation: 338

1

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

Somesh Mukherjee

Posted 2011-10-11T20:22:56.623

Reputation: 305

1

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.

Keith Thompson

Posted 2011-10-11T20:22:56.623

Reputation: 4 645

I'm trying to avoid looking at the shell to find out if a job finished. Thanks, though! – Utkarsh Sinha – 2011-10-11T22:20:18.367

1

On systems with 'awk' try

awk 'BEGIN{ print "\a" }'

was the only one to work for me.

Krzysztof

Posted 2011-10-11T20:22:56.623

Reputation: 11

1

That's just a verbose way of echoing \a (trinth's answer).

– David Richerby – 2015-09-04T08:05:21.297

0

Another possibility is to use alert, Works on Linux.

>any-command; alert

It gives a notification as in the image. alert notification

DTharun

Posted 2011-10-11T20:22:56.623

Reputation: 101

0

I made an app that will send a notification to your Android phone when a process ends so you don't have to be tethered to your computer.

Install, then add ;notifier.sh to the end of your command.

For example, if your command was make install, you would make your command:

make install; notifier.sh

https://notifier.sh

https://play.google.com/store/apps/details?id=sh.notifier

Jolt151

Posted 2011-10-11T20:22:56.623

Reputation: 1