[Solution provided] Login event sound Ubuntu 20.04

I’d love to get my Ubuntu 20.04 Framework to play me a sound when I log in. Sound is working ok fine when I am using the machine. And I believe I have the correct setting enabled:

But alas she is silent when I start my session. Anyone else had luck with this?

This is for the GNOME desktop. For KDE (Kubuntu), this will be your startup tool.

The best way is as follows if you want the traditional startup sound. I tested this myself, worked on 22.04, should be similar with 20.04. This will work on GNOME and KDE, except for the Start Up tool will differ (see above).

  • Start by creating a text file called sound. Paste in the following.
#!/bin/bash
sleep 1
paplay /usr/share/sounds/Yaru/stereo/system-ready.oga
  • Save the file as sound.sh in your home directory.

  • In your home directory, locate the file, right click it, goto permissions, check box executable.

  • (Ubuntu GNOME) Go to Activities, search Start Up, open that.

  • (Ubuntu GNOME) Click Add, name this Startup Sound. Enter this into the command box:

sh /home/username/sound.sh

  • Replace username with your actual user name. I tested $USER and it did not work, needs your actual user name.

  • (Ubuntu GNOME) Now that you’ve given this a Name and a Command, click Save.

  • Reboot to test.

Note, if this fails, double check your /home/username/sound.sh path has your correct Ubuntu user name, it is case sensitive.

Hah! Tried adding the paplay … line to my .bashrc, but then of course it plays every time I open a terminal session. But adding the shell script to the startup activities screen worked like a champ. Thanks man!

1 Like

Hah hah. Spoke too soon. This works great if I log out and then log back in while the machine is running. But it makes no sound on initial startup. Seems like there’s something weird in the way the mixer is initializing on boot.

BTW I found a fun archive of various startup sounds, so I wrote a script to pick a random one…


#!/bin/bash

###################################
#
# Play a random file from a directory of sound files
#
# Charles Shapiro 17 Dec 2022
#
#     This program is free software: you can redistribute it and/or
#     modify it under the terms of the GNU General Public License as
#     published by the Free Software Foundation, either version 3 of
#     the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program. If not, see
#    <https://www.gnu.org/licenses/>.
#
####################################


#
# Given a directory, get a random filename from it.
#
get_random_filename()
{
    NUMFILES=$(ls $1 | wc -l)
    FILENAME=$(ls $1 | nl -nln | awk /^$(($2 % ${NUMFILES} ))/'{ print $2 }')    
}


#
# Main Line
#

PLAYEXEC=$( which paplay)
OPTSTR="D:R:"
RAND=${RANDOM}
DIR=/home/devel/Boot_Sounds

while getopts ${OPTSTR} ARG
do
    case ${ARG} in
        D) DIR=${OPTARG} ;;
        R) RAND=${OPTARG} ;;
        *) echo Oops. Usage: ${OPTSTR}
    esac
done

get_random_filename $DIR  $RAND

# set -x
${PLAYEXEC} ${DIR}/${FILENAME}

Curiouser and curiouser. Tried putting a boot sound service into systemd. After some minor struggles (paplay won’t work there, but play will), this:

[Unit]
Description=Play sound on boot
After=sound.target
StartLimitIntervalSec=0

[Service]
Type=oneshot
ExecStartPre=
ExecStart=/home/devel/bin/play_random_sound.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Will happily play a sound from the root prompt with the command

systemctl start bootsound.service

But it is still silent when I reboot the machine. systemd status report it worked fine:

root@pufling:/home/devel/sound_systemd/etc/systemd/system# systemctl status bootsound.service
● bootsound.service - Play sound on boot
     Loaded: loaded (/etc/systemd/system/bootsound.service; enabled; vendor preset: enabled)
     Active: active (exited) since Sat 2022-12-17 11:44:20 EST; 1min 48s ago
    Process: 883 ExecStart=/home/devel/bin/play_random_sound.sh -R 39 (code=exited, status=0/SUCCESS)
   Main PID: 883 (code=exited, status=0/SUCCESS)
        CPU: 52ms

Dec 17 11:44:18 pufling play_random_sound.sh[911]: /home/devel/Boot_Sounds/Indy_-_Boot.ogg:
Dec 17 11:44:18 pufling play_random_sound.sh[911]:  File Size: 23.0k     Bit Rate: 86.9k
Dec 17 11:44:18 pufling play_random_sound.sh[911]:   Encoding: Vorbis        Info: http://archive.org/details/>
Dec 17 11:44:18 pufling play_random_sound.sh[911]:   Channels: 2 @ 16-bit
Dec 17 11:44:18 pufling play_random_sound.sh[911]: Samplerate: 44100Hz
Dec 17 11:44:18 pufling play_random_sound.sh[911]: Replaygain: off
Dec 17 11:44:18 pufling play_random_sound.sh[911]:   Duration: 00:00:02.11
Dec 17 11:44:19 pufling play_random_sound.sh[911]: [1.1K blob data]
Dec 17 11:44:19 pufling play_random_sound.sh[911]: Done.
Dec 17 11:44:20 pufling systemd[1]: Finished Play sound on boot.

But alas the machine is still silent on boot. I am suspecting that this is a mixer problemo???

Could be? Looks like you’re on KDE whereas I was on GNOME (Ubuntu official).

Also, which sound file are you attempting to play? I used /usr/share/sounds/Yaru/stereo/system-ready.oga

And I opted against using a systemd-service as I wanted to dumb down the process a bit with autostart (which works great on GNOME.

Did you try the auto-start process linked above for KDE? System Settings/Autostart using my exact script?

Once we establish autostart will play it, we can try systemd services and so forth. But we need to keep this hyper simple until it works, first.

Going to mark this as solved as it works under Ubuntu using GNOME using my suggestion above.

Will work if tossed into autostart.