12

I do sudo apt-get update from time to time, and find it frustrating to have to check when it is done doing its thing. Is there anyway I can get it to offer a notification popup or play a sound when it has finished executing, so I can get on with my work?

1

4 Answers 4

17

Sound I can't help you with. However, if you append something like

  && notify-send "Task complete."

after your command, you should see a notification pop up in the upper right corner. If the required package is not installed by default, sudo apt-get install libnotify-bin should get it for you. The && will execute a second command if the first completes successfully.

8

What you want is sudo apt-get update; alert. What follows is explanation and rationale.


  • From the terminal, you can list many commands to be run in sequence. So what you want is first the command which takes a long time, and second a notification command.
  • To run commands in sequence, you separate them with either && or ;. These have two distinct meanings: command-a && command-b means that command-a will run first, and then, if it was successful, command-b will run; command-a; command-b means that command-a will run first and then command-b will run, whether or not command-a was successful. You probably want the semi-colon: you want to be notified when the update or upgrade has stopped running, whether or not it has exited successfully.
  • Other answerers have given you suggested audio and on-screen alerts. My own preference is for notify-send, which takes two parameters: a title and content: sudo apt-get update; notify-send update finished (if either parameter contains spaces, enclose them in quotation marks).
  • Much quicker and easier to type than notify-send title content is alert. This alias is built into bash by default in at least some versions of Ubuntu, and is easy enough to add if it’s missing from yours (use type alert to check whether it’s present). You should get the response,

    alert is aliased to `notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e 's/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//')"'
    

    This means that it will simply pull out the last command and send it to notify-send, and that the accompanying icon will be an image of a terminal (for a successful command) or an error image (for a failed command).

    Life has now become even simpler: command; alert is very quick and easy to type. It will alert whether or not the command was successful, and the alert will contain an indication of whether or not the command was successful.

Incidentally, it has occasionally annoyed me that I am unable to pipe output to notify-send, so I wrote a little function to do it for me:

notify () 
{ 
    while read input; do
        notify-send -i $(ls /usr/share/icons/gnome/32x32/emotes | sed 's/\(.*\)\..*/\1/' | shuf -n1) "message" "$input";
    done
}

This allows me to type command | notify. The icon in this case is random, however. Also, you get a notification for each line of output, which is probably not a good choice for apt-get update.

2
  • Re piping input: Why not just use command substitution (i.e. backticks or the $() syntax)?
    – Kevin
    Apr 21, 2015 at 13:19
  • The way I did it was the easiest for me at the time; I'm not particularly proficient in Bash scripting.
    – TRiG
    Apr 21, 2015 at 13:24
4

For aplay compatible files

To play a custom sound through the speakers, you can run

aplay <path_to_sound_file>

*<path_to_sound_file> = path to the sound file

to play the chosen sound file; then you can add a playsound alias to ~/.bashrc:

alias playsound='aplay <path_to_sound_file>'

*<path_to_sound_file> = path to the sound file

This way you can run sudo apt-get update && playsound to play the chosen sound when sudo apt-get update has finished running.

For non aplay compatible files (e.g. .ogg files) (system sounds)

To play a custom sound through the speakers, you can run

pacmd list-sinks | grep -E 'index|device.product.name'

to list all pulseaudio's available sinks' indexes along with their corresponding device; then you can run

pacmd play-file /usr/share/sounds/ubuntu/stereo/bell.ogg <sink_index> > /dev/null

*<sink_index> = pulseaudio's sink's index

to play /usr/share/sounds/ubuntu/stereo/bell.ogg through the chosen sink. (Of course you can play any sound you want); then you can add a playsound alias to ~/.bashrc:

alias playsound='pacmd play-file /usr/share/sounds/ubuntu/stereo/bell.ogg <sink_index> > /dev/null'

*<sink_index> = pulseaudio's sink's index

This way you can run sudo apt-get update && playsound to play the chosen sound when sudo apt-get update has finished running.

3
  • 1
    aplay might offer a much simpler alternative.
    – muru
    Apr 21, 2015 at 8:02
  • @muru True, updated
    – kos
    Apr 21, 2015 at 8:07
  • @muru It seems not to be working for .ogg files tough
    – kos
    Apr 21, 2015 at 8:11
2

I wrote a little scripts to make it. Script is located in github.

Here is installation guide:

Installation

Download or just clone repo

 $ git clone git@github.com:c0rp-aubakirov/notify_after_command_executed.git

Then add sourcing of script postexec_notify to your .bashrc.

Here is git clone example of installation:

 $ cd ~
 $ git clone git@github.com:c0rp-aubakirov/notify_after_command_executed.git
 $ cd ~/notify_after_command_executed
 $ echo "source $(pwd)/postexec_notify" >> ~/.bashrc

Now restart your terminal. After restart you should see this messages:

preexec.sh is not exist
Trying to download it from github

Now try to test if notification appear:

$ sleep 6

Please let me know if something is unclear or you have any issues.

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