Alert when terminal program finishes running?
Solution 1
There's couple of ways, both with &&
(logical AND) operator (which runs only when last command succeeded). If you want to run notification regardless of whether command succeeded or failed, use semicolon ;
instead of &&
.
-
Graphical
myscript.sh && notify-send 'DONE'
-
Audio:
myscript.sh && aplay /usr/share/sounds/speech-dispatcher/test.wav
Note, you can use any audio file instead of the one I used here.
-
Both:
myscript.sh && aplay /usr/share/sounds/speech-dispatcher/test.wav && notify-send 'DONE !'
Solution 2
You can use zenity to display a popup.
After your shell command, append
&& zenity --info --text "STRING"
Solution 3
I suggest using libnotify's notify-send
for notifications. I've created the following script to do this:
#!/bin/bash
$1 && \
notify-send -u critical -i info 'Command execution finished' "The command '$1' terminated successfully" || \
notify-send -u critical 'Command execution failed' "The command '$1' exited with errors"
If you save above script in a file called e.g. nexec
and make it executable using chmod +x nexec
, and move the program to a directory in your shells PATH
variable, (e.g. /usr/local/bin/
), you can run any command with
nexec 'long-time-command some arguments'
E.g.
nexec 'sudo apt-get update && apt-get dist-upgrade'
libnotify will (at least it does so on my system, I assume this can differ between different desktops) display notifications marked as 'critical' until they are being dismissed manually (e.g. by clicking on them). As you can take from the code, you will receive a different notification if the command returns a non-zero result.
Solution 4
Have you ever heard of undistract-me? It seems it fits your needs and its code it's on github.
I think the package is in the official Ubuntu repos, so should be sufficient
sudo apt-get install undistract-me
then you have to close and reopen any terminal you have, just to let the changes take effect and you can test it with a simple
sleep 11
Remember to change your active window, otherwise you won't see any notification.
Related videos on Youtube
![becko](https://i.stack.imgur.com/7LxPC.jpg?s=256&g=1)
becko
Updated on September 18, 2022Comments
-
becko almost 2 years
I am running Xubuntu 15.04, but maybe this question has a general solution for all Ubuntu flavours.
I usually leave long computations running in a terminal window, which may take from a couple of minutes, to hours, or days. It would be nice if I could be alerted somehow when a terminal finishes executing a command. This should only be necessary for terminals which are minimized.
Is there a way to set up something like this? Ideally, the terminal window could flash, or maybe a notification show up.
-
Rens van der Heijden almost 9 yearsI'm not sure if its an option, but you could use the terminal bell to do this (
echo '\a'
) with sound, see en.wikipedia.org/wiki/Bell_character -
Rens van der Heijden almost 9 yearsRight. I guess it's better than nothing -- other solutions I can think of (such as libnotify) have the same problem. I'm not sure if
xfce4-notifyd
offers a blinking-until-focus feature, maybe someone with more xfce knowledge can use that to give you a proper answer, though. -
muru almost 9 years
-
-
becko almost 9 yearsCan I customize the message?
-
Bacon almost 9 yearsYes, with the option text="STRING"
-
Buck almost 9 yearsI tried sending "echo '\a' && zenity --info" but all I got was the terminal displaying "\a" and a popup window saying update was complete. I believe that if you want a visible notice, the && zenity --info addition will do what you want. It will be there till you return and hit "enter".
-
becko almost 9 years
notify-send 'DONE'
produces a balloon which disappears after a time interval. If I am away from keyboard for a while, I might miss the alert and never find out that the program terminated unless I manually check. Is there a way to make the balloon stay indeterminately, until it is clicked or something? -
user35952 almost 9 years@becko : There is option of
expire-time=TIME
for the same, I think we can use that. -
asgs over 7 yearsZenity only seems to display the popup within the Terminal window. If I have switched to something else, I'm not seeing the popup. Is this expected? Or am I doing something wrong?
-
asgs over 7 yearsThanks! The
-u critical
makes the notification persistent until we click on it.