25

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.

4
  • I'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 Aug 4 '15 at 20:28
  • @RensvanderHeijden The problem with that solution is that if I get away from the PC for a few minutes, and the program happens to finnish, I will miss the bell and won't realize that the program finished. – becko Aug 4 '15 at 20:29
  • Right. 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. – Rens van der Heijden Aug 4 '15 at 20:32
34

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

  1. Graphical

    myscript.sh && notify-send 'DONE'
    
  2. 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.

  3. Both:

     myscript.sh && aplay /usr/share/sounds/speech-dispatcher/test.wav && notify-send 'DONE !'
    
2
  • 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? – becko Aug 5 '15 at 12:20
  • @becko : There is option of expire-time=TIME for the same, I think we can use that. – user35952 Sep 22 '15 at 11:23
12

You can use zenity to display a popup.

After your shell command, append

&& zenity --info --text "STRING"
4
  • Can I customize the message? – becko Aug 4 '15 at 20:34
  • Yes, with the option text="STRING" – Bacon Aug 4 '15 at 20:36
  • I 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". – Buck Aug 4 '15 at 20:40
  • Zenity 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 Mar 21 '17 at 7:11
4

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.

1
  • 2
    Thanks! The -u critical makes the notification persistent until we click on it. – asgs Mar 21 '17 at 7:21
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.

1
  • Once installed, how can it be deactivated, if I don't want it for some time ? – user35952 Sep 22 '15 at 11:08

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