Score:3

How to play a sound from a systemd service on Ubuntu?

in flag

I try to play a sound from a service on Ubuntu:

tim@timpc:~$ uname -a Linux timpc 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Here is the service definition:

tim@timpc:~$ cat /etc/systemd/system/alarmd.service
# Datei /etc/systemd/system/alarmd.service
[Unit]
Description=alarmd service
Wants=sound.target
After=sound.target

[Service]
Type=oneshot
Environment="DISPLAY=:0"
User=tim
;ExecStart=/home/tim/Dokumente/super/alarmd/build/release/dist/alarmd --sound /usr/local/share/alarmd/a.wav
ExecStart=mplayer /usr/local/share/alarmd/a.wav

[Install]
WantedBy=multi-user.target

I start the service like this:

sudo systemctl start alarmd.service

...and get this output:

tim@timpc:~$ journalctl -S today -u alarmd.service
-- Logs begin at Wed 2021-08-11 09:41:59 CEST, end at Sun 2021-12-05 16:28:06 CET. --
Dez 05 16:28:06 timpc systemd[1]: Starting alarmd service...
Dez 05 16:28:06 timpc mplayer[13082]: MPlayer 1.3.0 (Debian), built with gcc-9 (C) 2000-2016 MPlayer Team
Dez 05 16:28:06 timpc mplayer[13082]: do_connect: could not connect to socket
Dez 05 16:28:06 timpc mplayer[13082]: connect: No such file or directory
Dez 05 16:28:06 timpc mplayer[13082]: Failed to open LIRC support. You will not be able to use your remote control.
Dez 05 16:28:06 timpc mplayer[13082]: Terminal type `unknown' is not defined.
Dez 05 16:28:06 timpc mplayer[13082]: Playing /usr/local/share/alarmd/a.wav.
Dez 05 16:28:06 timpc mplayer[13082]: libavformat version 58.29.100 (external)
Dez 05 16:28:06 timpc mplayer[13082]: Audio only file format detected.
Dez 05 16:28:06 timpc mplayer[13082]: Load subtitles in /usr/local/share/alarmd/
Dez 05 16:28:06 timpc mplayer[13082]: ==========================================================================
Dez 05 16:28:06 timpc mplayer[13082]: Opening audio decoder: [pcm] Uncompressed PCM audio decoder
Dez 05 16:28:06 timpc mplayer[13082]: AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Dez 05 16:28:06 timpc mplayer[13082]: Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
Dez 05 16:28:06 timpc mplayer[13082]: ==========================================================================
Dez 05 16:28:06 timpc mplayer[13082]: AO: [pulse] Init failed: Connection refused
Dez 05 16:28:06 timpc mplayer[13082]: Failed to initialize audio driver 'pulse'
Dez 05 16:28:06 timpc mplayer[13082]: [AO_ALSA] alsa-lib: pcm_hw.c:1715:(snd_pcm_hw_open) open '/dev/snd/pcmC0D0p' failed (-2): No such file or directory
Dez 05 16:28:06 timpc mplayer[13082]: [AO_ALSA] alsa-lib: pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
Dez 05 16:28:06 timpc mplayer[13082]: [AO_ALSA] Playback open error: No such file or directory
Dez 05 16:28:06 timpc mplayer[13082]: Failed to initialize audio driver 'alsa'
Dez 05 16:28:06 timpc mplayer[13082]: [AO SDL] Samplerate: 44100Hz Channels: Stereo Format s16le
Dez 05 16:28:06 timpc mplayer[13082]: [AO SDL] using aalib audio driver.
Dez 05 16:28:06 timpc mplayer[13082]: [AO SDL] Unable to open audio: No available audio device
Dez 05 16:28:06 timpc mplayer[13082]: Failed to initialize audio driver 'sdl:aalib'
Dez 05 16:28:06 timpc mplayer[13082]: Could not open/initialize audio device -> no sound.
Dez 05 16:28:06 timpc mplayer[13082]: Audio: no sound
Dez 05 16:28:06 timpc mplayer[13082]: Video: no video
Dez 05 16:28:06 timpc mplayer[13082]: Exiting... (End of file)
Dez 05 16:28:06 timpc systemd[1]: alarmd.service: Succeeded.
Dez 05 16:28:06 timpc systemd[1]: Finished alarmd service.

How should I define the service so that the sound is played?

The command itself works:

tim@timpc:~$ mplayer /usr/local/share/alarmd/a.wav
MPlayer 1.3.0 (Debian), built with gcc-9 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing /usr/local/share/alarmd/a.wav.
libavformat version 58.29.100 (external)
Audio only file format detected.
Load subtitles in /usr/local/share/alarmd/
==========================================================================
Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
==========================================================================
AO: [pulse] 44100Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A:   9.1 (09.1) of 233.0 (03:53.0)  0.0% 


MPlayer interrupted by signal 2 in module: play_audio
A:   9.2 (09.1) of 233.0 (03:53.0)  0.0% 

Exiting... (Quit)
Marc Vanhoomissen avatar
in flag
Could you add `WantedBy=sound.target` in the `[Install]` section?
in flag
'WantedBy=sound.target' does not help unfortunately
user10489 avatar
in flag
You may want to make this a user systemd file in your account instead of a system level file, so it starts after your login session is available. If you want to play sound when no user is logged in, the answer is different.
Score:3
in flag

In current versions of Ubuntu, typically sound is handled by the pulse audio server. If you run sound players from your logged in session, then the player will find the server through the environment variables XDG_RUNTIME_DIR and/or PULSE_RUNTIME_PATH. For a job in systemd (or cron) to be able to find the server, it will need these values.

Luckily, those values are predictable via the user name, and should not change from one login to the next if it is the same user. (Setting DISPLAY was a good try, but not the right session variable.)

For example, if user tim is the first user on the system, the user's uid is likely 1000 (check in /etc/passwd or type id or check current values.) Add to your systemd file the following: (Not sure if one of these is good enough or if you need both, it may be version dependent.)

Environment="XDG_RUNTIME_DIR=/run/user/1000"
Environment="PULSE_RUNTIME_PATH=/run/user/1000/pulse/"

Since this only works when you are logged in anyway, an alternative to a systemd file would be to start your service in a script that is started as part of your gnome-session. If you did this, you wouldn't need to worry about the session variables at all.

in flag
Thank you very much!
Score:0
cn flag

I would comment this but I don't have enough reputation points, so please excuse me for leaving a link without some kind of explanation (even though this is described as a bad answer in the posting guide).

This link may help. It explains something similar, which is how you can start an app or process using systemd. if you can find a command for playing a sound file and place it systemd, you will be able to achieve what you want.

link is here (the top answer will provide the steps required to add a command to systemd)

Keep in mind that editing systemd could make your computer un-bootable if done incorrectly, so maybe test this solution first in a virtual machine.

EDIT: You can play sound files from bash using these commands.

in flag
the command itself works. See the output in my question.
Score:0
ph flag

I know I'm a bit late to the party, but one option in Ubuntu (server) is the rplay service:

From: https://launchpad.net/ubuntu/lunar/+package/rplay-client

rplay network audio system - basic clients This package contains the basic rplay clients (rplay, rptp). The clients are used to play sounds on the local host or remote systems. . RPlay allows sounds to be played to and from local and remote Unix systems. Sounds can be played with or without sending audio data over the network using either UDP or TCP/IP. RPlay audio servers can be configured to share sound files with each other.

See also: https://manpages.ubuntu.com/manpages/lunar/en/man1/rplay.1.html

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.