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.
613 Answers
I use
make; spd-say doneReplace "make" with whatever long-running command you use.
5There 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:
The "classical" way to play a sound is to use beep.
Beep will make a tone through the PC speaker. However this will not work in all cases (e.g. in my system PC speakers are completely disabled) You may have to remove
pcspkrfrom/etc/modprobe/blacklist.confand load thepcspkrkernel module:sudo sed -i 's/blacklist pcspkr/#blacklist pcspkr/g' /etc/modprobe.d/blacklist.conf sudo modprobe pcspkr beep [optional parameters]We can also play any sound file in wav format using aplay (installed by default):
aplay /usr/share/sounds/alsa/Side_Right.wavAnother way is to use the pulseaudio command line interface to enable playback of any sound files your system (in
libsndfile) recognizes on the default audio output:paplay /usr/share/sounds/freedesktop/stereo/complete.oga
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, or alternatively use spd-say "Text".
According to this the \a character escapes ASCII code 7, which is the computer's beep.
So echo $'\a' works to make a beep sound on my local machine, even when it's executed on a bash shell running on a computer I'm connected to via a terminal interface like PuTTY.
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; oksIt 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/functionConfiguration
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; doneHere 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 - here 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; oksexample with error:
ls / && ls /bim && ls /usr; oksOf course in practice the commands are more like:
make && make test && make install; oksbut 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; sn1to print OK/ERROR but always play the same sound, etc.
Update
I put those functions on GitHub, see:
The source can be downloaded from:
Update 2
I added a soundloop function to the above repo. It plays a sound and can be interrupted by Ctrl+C (unlike a simple while true; do paplay file.ogg; done that one would expect to work but it doesn't) as asked by shadi in the comments. It is implemented as:
soundloop() { set +m a=`date +%s` { paplay $1 & } 2>/dev/null wait b=`date +%s` d=$(($b-$a)) [ $d -eq 0 ] && d=1 while :; do pacmd play-file $1 0 >/dev/null sleep $d done
}If you think it is complicated, please direct your complains to PulseAudio developers.
4The command
speaker-testmakes a noise sound. Simplest but annoying solution. :-)
Look at the manual of speaker-test(1) for options to configure the noise signal.
Update
If you do not mind installing sox, you can play Silicon Heaven Hymn:
sudo apt install sox
while true; do play -q -v 100 -n synth .$(( $RANDOM % 51 ))1 sine $((200 + $RANDOM % 4000 )) &> /dev/null; doneIf you do not mind to install espeak:
sudo apt install espeak
espeak 'Johnny is marching home! Hurray!'
espeak -v german 'Doch! Ich bin vertig!'
espeak -x -v czech 'Běžela Magda kaňonem, srážela banány ramenem.' Expanding on Michael Curries's answer, you could make Bash print a BEL (\a) character through PROMPT_COMMAND:
PROMPT_COMMAND='printf \\a'Setting PROMPT_COMMAND that way will make Bash execute printf \\a at the end of each command, which will make the terminal play a sound (though as muru points out, simply triggering the redrawal of the prompt will make the terminal play the sound, i.e. the sound will be played each time a new prompt is drawn, for example even when just hitting ENTER).
This is a terminal feature, so it might not work across all terminals; for example it doesn't work in the console (but I'm sure it works in gnome-terminaland xterm).
This is not what you asked but you could use notification for that.
Replace the command given in the other answers with
notify-send "Process terminated" "Come back to the terminal, the task is over" 1 ffmpeg sine wave
For the minimalists out there, you can play a 1000 Hz sine for 5 seconds:
sudo apt-get install ffmpeg
ffplay -f lavfi -i "sine=frequency=1000:duration=5" -autoexit -nodispor forever until you do Ctrl-C:
ffplay -f lavfi -i "sine=frequency=1000" -nodispMore information at:
Tested in Ubuntu 18.04, ffmpeg 3.4.6.
2Super simple answer:
Play the ASCII Bell Character sound:
echo -e "\a"Do it after a long command, emulated by sleep 2 here:
sleep 2; echo -e "\a"DONE!
More details:
To make this easy to use, add this to the bottom of your ~/.bashrc file (create this file if it doesn't exist). Or, if using Ubuntu, add it to your ~/.bash_aliases file instead (which file is imported by Ubuntu's default ~/.bashrc file) like I do in my ~/.bash_aliases file here:
# Play sound; very useful to add to the end of a long cmd you want to be notified of when it completes!
# Ex: `long_cmd; gs_sound_bell` will play a bell sound when `long_cmd` completes!
alias gs_sound_bell="echo -e \"\a\""
# Even better, have a pop-up notification too!
# Ex: `long_cmd; gs_alert` will play the sound above *and* pop up a notification when complete!
# See more details & a screenshot of the popup on my answer here:
#
# For other popup window options, see my other answer here:
#
alias gs_alert="gs_sound_bell; alert \"task complete\""Notice that my gs_alert alias calls alert, so you need that alias defined too. This alias is defined by default in Ubuntu. But, if your ~/.bashrc file doesn't already have this near the top of it, then add it just above the code above. Again, these lines come default in your ~/.bashrc file in Ubuntu 18 or 20, for instance:
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'Test it:
Close and re-open your terminal, or call . ~/.bashrc to re-"source" the file, then test it like this:
sleep 2; gs_alertAfter 2 seconds you'll hear the bell sound and see this pop up at the top of your screen:
If you ever forget what your alias contains, you can see what's in each one by running this:
# See the definitions for ALL aliases you have defined!
alias
# See just the definitions for specific aliases. Ex:
alias gs_sound_bell
alias gs_alert
alias alert Use it:
# get alerted when `my_long_cmd` finishes! Ex: building software.
my_long_cmd; gs_alert Notes:
- The
gs_part at the beginning of each alias is my initials. I prepend my custom commands and aliases with this to make them easy to find: I can now just typegs_at the terminal then press Tab Tab (Tab twice) to see all my custom commands print out.
Going Further:
- If you find this useful, you'll find a lot more of my Linux settings, including everything above, here: .
- Check out my
homedirectory.
- Check out my
- For other window popup options, see my other answer here: SuperUser.com: Is there a way to show notification from bash script in Ubuntu?
You can press Ctrl + g on your keyboard while on the terminal window that is running the command and when the command finishes it'll beep. If you press it multiple times it will beep many times as well.
command && (say done ; echo done) || (echo error ; say error)Example 1: echo alik && (say done ; echo done) || (echo error ; say error) will result in a done word.
Example 2: non_existing_command_error && (say done ; echo done) || (echo error ; say error) will result in an error word.
* Needs gnustep-gui-runtime -
sudo apt-get install gnustep-gui-runtimeCheers.
Just adding an interesting idea i've seen implemented in some scripts, using the system bell:
tput belit will ring the system bell, which (at least in konsole) shows a notification if you're not watching the terminal and does nothing if you are watching it (in old termnals it might ring an actual bell). according to this quesiton it is very portable, it even works in macOS and doesn't need anything to be installed.
I created a simple and almost-native script that plays Sound and displays a Notification with a Given Message and Time for Ubuntu (Gist):
#!/bin/sh
#
# Create a Notification With Sound with a Given Message and Time
# The Downloaded Sound is from Notification Sounds
MSSG="$1"
TIME="$2"
# install wget if not found
if ! [ -x "$(command -v wget)" ]; then echo -e "INSTALLING WGET...\n\n" sudo apt-get install wget echo -e "\n\n"
fi
# install at package if not found
if ! [ -x "$(command -v at)" ]; then echo -e "INSTALLING AT...\n\n" sudo apt-get install at echo -e "\n\n"
fi
# install sox if not found
if ! [ -x "$(command -v sox)" ]; then echo -e "INSTALLING SOX...\n\n" sudo apt-get install sox sudo apt-get install sox libsox-fmt-all echo -e "\n\n"
fi
# download the noti sound if this is first time
# add alias to the bashrc file
if ! [ -f ~/noti/sound.mp3 ]; then echo -e "DOWNLOADING SOUND...\n\n" touch ~/noti/sound.mp3 | wget -O ~/noti/sound.mp3 "" sudo echo "alias noti=\"sh ~/noti/noti.sh\"" >> ~/.bashrc source ~/.bashrc echo -e "\n\n"
fi
# notify with the sound playing and particular given message and time
echo "notify-send \""$MSSG\"" && play ~/noti/sound.mp3" | at $TIMEHow To Use?
First Run - Setting Up:
Create a new Directory at your home and call it
notimkdir ~/notiDownload noti.sh and extract it to the above
notidir.Open Terminal and Change Directory to
noticd ~/notiMake noti.sh executable by issuing:
sudo chmod +x noti.shRun a Test like this:
sh ~/noti/noti.sh "Test" "now"
Examples
noti "Hello From Noti" "now +1 minute"
noti "Hello From Noti" "now +5 minutes"
noti "Hello From Noti" "now + 1 hour"
noti "Hello From Noti" "now + 2 days"
noti "Hello From Noti" "4 PM + 2 days"
noti "Hello From Noti" "now + 3 weeks"
noti "Hello From Noti" "now + 4 months"
noti "Hello From Noti" "4:00 PM"
noti "Hello From Noti" "2:30 AM tomorrow"
noti "Hello From Noti" "2:30 PM Fri"
noti "Hello From Noti" "2:30 PM 25.07.18"For Notifying The Finish of Process (example)
sudo apt-get update; noti "Done" "now"