1. Introduction

Efficiently managing Linux processes and tasks often involves more than just monitoring them visually; audible notifications can be invaluable. When it comes to tracking the completion of various operations or scripts in a Linux environment, playing a sound at the end of a process can serve as a useful signal.

In this tutorial, we’ll explore different methods to implement such sound notifications effectively.

Notably, the volume level may need adjustment to ensure that we hear the sound notifications effectively. However, it’s worth noting that with some methods, this adjustment might not be required. That usually depends on whether we use the internal PC speaker.

2. Using Bell Sound

Playing a bell sound at the end of a Linux process is a simple way to get an audible notification when the process completes. Alternatively, this can be useful for monitoring long-running tasks or as a signal for various events.

2.1. Using echo

The echo command is one of the simplest ways to produce a bell sound at the end of a Linux process. We can use the ASCII bell character \a or hex character \007 to trigger the sound:

$ ./my_script.sh ; echo -e "\a"

In this instance, my_script.sh represents the script or desired command that’s going to be the basis of a process. The semicolon separates the script execution from echo, ensuring that the bell sound plays after the process completes.

Importantly, echo can interpret escape sequences within the output string due to the -e option.

2.2. Using printf

Using printf for playing a sound at the end of a Linux process is a similarly straightforward method. It generates a bell sound, serving as an audible notification upon process completion.

Let’s see the usage of printf for this goal:

$ ./my_script ; printf "\a"

In this section, my_script.sh runs and, once it finishes, the printf command is executed, resulting in the production of the bell sound. Of course, we can also use \007 which is the hex character of the bell character in hex format.

2.3. Using tput

The tput command is a utility for interacting with and querying various terminal capabilities. Alternatively, we can use it to play an audible notification once the specified Linux process or command has finished executing.

$ ./my_script.sh ; tput bel

In this case, we sent the bel command to the tput, which instructs the terminal to produce a bell sound.

2.4. Using speaker-test

speaker-test is typically used to test and configure audio output on the system, specifically for sound cards and speakers.

Let’s use speaker-test to play a sound after the process completion:

$ ./my_script.sh ; speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $!

Let’s break down this example:

  • ./my_script.sh is again our script or command
  • ; separates multiple commands on a single line
  • -t sine specifies that speaker-test should generate a sine wave
  • -f 800 sets the frequency of the sine wave to 800Hz
  • & places a process in the background
  • sleep .2 pauses the execution of the shell script for 0.2 seconds
  • kill -9 $! kills the last backgrounded process via its $! process ID to stop the sound

In summary, we execute a shell script named my_script.sh. After this script has finished executing, the speaker-test command runs in the background, generating a sine wave sound. This occurs for the sleep duration of 0.2 seconds and then the script forcefully terminates the speaker-test process using the kill command and $!. So, the sound is played briefly after my_script.sh has completed its execution.

3. Playing a Sound File

We can also play a sound file in Linux processes for effective auditory notifications.

3.1. Using aplay

aplay is a command-line audio player for Linux systems that is part of the Advanced Linux Sound Architecture (ALSA) project. Hence, aplay can be a simple and efficient way to handle audio playback tasks on the command line.

Let’s play a sound via aplay:

$ ./my_script.sh ; aplay /home/amir/Downloads/beep-01a.wav 
Playing WAVE '/home/amir/Downloads/beep-01a.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono

Here, we run a shell script named my_script.sh. After the script finishes its execution, plays an audio file using the aplay command, with the displayed output providing information about the audio file being played.

3.2. Using paplay

paplay is a command-line tool on Linux that enables us to play audio files using the PulseAudio sound server. Generally, it enables us to play custom sounds or audio files of our choice as a part of the Linux scripts or processes.

First, let’s install paplay via apt-get and sudo:

$ sudo apt-get install pulseaudio

Now, we use paplay to play a local sound file:

$ ./my_script.sh; paplay /home/amir/Downloads/beep.wav

In this case, the Linux system first executes my_script.sh. Then, once that process is completed, it plays the beep.wav sound using the paplay command. Therefore, the audio notification indicates that the script or process has finished running.

4. Using Text-To-Speech

spd-say is a Linux command that makes the computer speak a text message out loud. Hence, we can use it to audibly notify us when a task or process is complete.

Initially, let’s install spd-say:

$ sudo apt-get install speech-dispatcher

spd-say is part of the speech-dispatcher package.

Let’s use spd-say to read the text:

$ ./my_script.sh; spd-say "Your process is complete."

In this case, my_script.sh is the process and once the script is done, it uses spd-say to announce the sentence.

5. Conclusion

In this article, we’ve explored various methods to implement sound notifications effectively into Linux processes. Starting with the fairly straightforward use of basic commands, we then delved into more advanced tools.

Consequently, by streamlining Linux system management, we can easily stay informed about task completion and critical events. Therefore, these auditory cues offer a practical and efficient way to enhance our workflow and overall system administration experience.

Comments are closed on this article!