Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I have started a long process through a terminal. Is it possible to make the Ubuntu terminal make a sound once the process is complete? This way, I don’t need to keep checking, but will instead be notified through a sound.

share|improve this question
    
Can you explain further what do you mean with process complete? –  Lucio Apr 2 '13 at 18:21
    
@Lucio control is returned back to the terminal –  Goaler444 Apr 2 '13 at 18:23
1  
Do you mean, open an application from terminal and when it finish, make a sound? Do you have an Ubuntu server or you mean GUI software? –  Lucio Apr 2 '13 at 18:24
2  
yes exactly. For example i start a command line program, and once it exits and control is returned back to the terminal, a sound is made. I am currently using Ubuntu 12.10 –  Goaler444 Apr 2 '13 at 18:31

3 Answers 3

up vote 24 down vote accepted

There are at least three command line ways to accomplish this by putting the suiting command at the end of your script you may invoke for your lengthy process:

  1. The "classical" way to play a sound is to use beep Install beep through the PC speakers. However this will not work in all cases (e.g. in my system PC speakers are completely disabled).

    beep [optional parameters]
    
  2. We can also play any sound file in wav format using aplay (installed by default):

    aplay /usr/share/sounds/alsa/Side_Right.wav
    
  3. Another way is to use the pulseaudio command line interface to enable playback of any sound files your system recognizes:

     pacmd play-file /path/to/sound.file <index/name>
    

    Replace the <index/name> with the index or name of output sink desired, e.g.:

     pacmd play-file /usr/share/sounds/freedesktop/stereo/complete.oga 1
    

We can use default sound files from /usr/share/sounds/, or any other sound file we may have in a different location.


Just to have mentioned it, there is another nice way to achieve this by misusing espeak, which is installed by default in Ubuntu <= 12.04. See, or rather hear the following example:

#! /bin/bash

c=10; while [ $c -ge 0 ]; do espeak $c; let c--; done; sleep 1 ## here lengthy code
espeak "We are done with counting"

In Ubuntu >= 12.10 Orca uses speak-dispatcher. We can then install espeak Install espeak, or alternatively use spd-say "Text".

share|improve this answer
    
thanks, went for option 2 :) –  Goaler444 Apr 2 '13 at 19:42
1  
@Takkat I'm using Ubuntu 12.10 and espeak is currently not installed. –  Lucio Apr 2 '13 at 20:55
    
@Lucio: yeah, now that you say it... They switched to speak-dispatcher. If this works (it does not on my system) see edit for using spd-say. –  Takkat Apr 2 '13 at 21:07
    
@Takkat The spd-say utility is installed by default and it works on my system. –  Lucio Apr 2 '13 at 21:09
    
Also take a look to this Q/A –  c0rp Apr 22 at 5:32

I use

make; spd-say done

Replace "make" with whatever long-running command you use.

share|improve this answer
    
You just made my day!! :D –  n0noob Jun 11 at 17:50

TL;DR

To play a sound after command finishes:

long-running-command; sn3

(where sn3 is sound number 3) put this in .bashrc:

sound() {
  # plays sounds in sequence and waits for them to finish
  for s in $@; do
    paplay $s
  done
}
sn1() {
  sound /usr/share/sounds/ubuntu/stereo/dialog-information.ogg
}
sn2() {
  sound /usr/share/sounds/freedesktop/stereo/complete.oga
}
sn3() {
  sound /usr/share/sounds/freedesktop/stereo/suspend-error.oga
}

Or read below for more options:

Different sound on error and success

Here is what I use for exactly what you ask for - with one difference: it not only plays a sound when the command finishes but it plays a different sound on success and on error. (But you can change it if you don't like it.)

I have a Bash function called oks that I use at the end of long running commands:

make && make test && make install; oks

It plays a sound and displays OK or ERROR (with error code) when the previous command finishes.

Source code

Here is that function with two helpers:

sound() {
  # plays sounds in sequence and waits for them to finish
  for s in $@; do
    paplay $s
  done
}
soundbg() {
  # plays all sounds at the same time in the background
  for s in $@; do
    # you may need to change 0 to 1 or something else:
    pacmd play-file $s 0 >/dev/null
  done
}
oks() {
  # like ok but with sounds
  s=$?
  sound_ok=/usr/share/sounds/ubuntu/stereo/dialog-information.ogg
  sound_error=/usr/share/sounds/ubuntu/stereo/dialog-warning.ogg
  if [[ $s = 0 ]]; then
    echo OK
    soundbg $sound_ok
  else
    echo ERROR: $s
    soundbg $sound_error
  fi
}

Installation

You can put it in your ~/.bashrc directly or put it in some other file and then put this line in your ~/.bashrc:

. ~/path/to/file/with/function

Configuration

Change sound_ok and sound_error to some other sounds.

You can experiment with sound vs. soundbg and change sound_ok and sound_error to use sequences of many sounds that you like to get the result that you want.

Good sounds

To find some good sounds on your system you can try:

for i in /usr/share/sounds/*/stereo/*; do echo $i; paplay $i; sleep 1; done

Here are some sounds that I often use that are available on Ubuntu by default that are good for notifications - sn1 is loud and nice, sn2 is very loud and still pretty nice, sn3 is extremely loud and not so nice:

sn1() {
  sound /usr/share/sounds/ubuntu/stereo/dialog-information.ogg
}
sn2() {
  sound /usr/share/sounds/freedesktop/stereo/complete.oga
}
sn3() {
  sound /usr/share/sounds/freedesktop/stereo/suspend-error.oga
}

Again, you can change sound to soundbg if you want to play it in the background without waiting for the sound to finish (e.g. to not slow down your scripts when you play a lot of sounds).

Silent version

And just in case this is the same function as oks but without sounds:

ok() {
  # prints OK or ERROR and exit status of previous command
  s=$?
  if [[ $s = 0 ]]; then
    echo OK
  else
    echo ERROR: $s
  fi
}

Usage

Here is how you use it:

Example with success:

ls / && ls /bin && ls /usr; oks

example with error:

ls / && ls /bim && ls /usr; oks

Of course in practice the commands are more like:

make && make test && make install; oks

but I used ls so you could quickly see how it works.

You can use ok instead of oks for a silent version.

Or you can use e.g.:

ls / && ls /bim && ls /usr; ok; sn1

to print OK/ERROR but always play the same sound, etc.

Update

I put those functions on GitHub, see:

The source can be downloaded from:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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