Play a sound at Linux startup

When Linux boot and as soon the sound system is up we can play automatically a sound. Sound should be in any format manageble from ffmpg. To do that we need to create a proper system service.

First of all it is better to find the ALSA device that play sound in our system and test playing trough that device works:

aplay -L

This should give a list of devices, choose the one that say Analog, but in any case better to test that it works

ffmpeg -loglevel error -i $FULL_PATH_TO_YOUR_SOUND -f alsa plughw:0,0

Where 0,0 is the card and device number of my analog output from aplay -L. Replace with correct numbers for yours

Now we can create the bash scrip will play the sound:

nano /usr/local/bin/boot-sound.sh

Add the command to play the sound file:

#!/bin/bash
sleep 1
/usr/bin/ffmpeg -loglevel error -i $FULL_PATH_TO_YOUR_SOUND -f alsa plughw:0,0

The sound system may be not immediatly ready, so I added 'sleep 1'. Tune the slip time according to you system.

Create the service description file:

sudo nano /etc/systemd/system/boot-sound.service

Then add the service description:

[Unit]
Description=Play Linux startup sound
After=sound.target

[Service]
Type=oneshot
User=YOUR USERNAME
SupplementaryGroups=audio
ExecStart=/usr/local/bin/boot-sound.sh

[Install]
WantedBy=multi-user.target

We can finally enable the service:

sudo systemctl daemon-reload
sudo systemctl enable boot-sound.service
sudo systemctl start boot-sound.service

and check it started properly:

sudo systemctl status boot-sound.service