How can I trigger a notification when a job/process ends?
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 isgxmessage
.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 oflibnotify
).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