How can i get sound when script executed via systemd service?

i have a systemd timer fire a service to do some backups.

I just wanted a notification, popup and sound.

I have managed to add the following to the top of my script, the notify-send works but no mater what i try i can not get sound to play

# Set environment variables to allow notify-send to work
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"

# Send notification as the logged-in user
sudo -u greg DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS notify-send "Backup Started" "BTRFS backup has started."
sudo -u greg /usr/bin/play '/home/greg/sounds/winsounds/Windows Message Nudge.wav'

sudo requires interaction by a user.

A common error is to think that systemd services should run as root

Create a user service in ~/.config/systemd/user/ - it will do what you want.

You could look at this topic to get an idea

Another example is

4 Likes

Ok, i had always assumed it did, and i double checked with gpt

Will it run the backup as root ? if not i don’t see how i can use it.

I just want a single command (or as near to) to add to the top of my script. If it means creating separate script and separate services etc just to make a sound then I shan’t worry about it.

I’ve never used it…

sudo -u tdell notify-send test                                                                                                                                                                                                                              
Error spawning command line “dbus-launch --autolaunch=f839d442d6194ae1b9148eead1d88b39 --binary-syntax --close-stderr”: Child process exited with code 1
sudo -u tdell -E notify-send test

But that works.

Thx, but my notify-send works, its a notify sound that i cant get to work.

Try this to play sound notification via pulseaudio or pipewire-pulse

/usr/bin/paplay '~/sounds/winsounds/Windows Message Nudge.wav'

manpages.org - paplay: Play back audio files on a PulseAudio sound server

I know nothing about pulseaudio server, but the command works in my shell but if i put it into my backup script that is run via systemd it gives this error

Connection failure: Connection refused
Aug 21 19:42:49 greg-venusseries backup-test.sh[309428]: pa_context_connect() failed: Connection refused

which is pretty much what all “play, aplay, etc” do

I was hoping for a missing environment variable. I guess I should of tried from a unit.

I’d use aplay before. But I know I’ve had many problems with PA permissions in the past connecting to the local socket. The most common is just refusing connections from root.

sudo -u $USER -E pw-cat -p example.wav Is the same thing?

sudo -u greg -E pw-cat -p '/home/greg/sounds/winsounds/Windows Message Nudge.wav'

Unfortunately, its the same.

 error: pw_context_connect() failed: Host is down
Aug 21 19:59:53 greg-venusseries sudo[314582]: pam_unix(sudo:session): session closed for user greg

If gpt would act intentional and on it’s own, it would be a liar.
… it is just not “intelligent” - you have to vet and double check it’s “answers” just like any internet search result …

2 Likes

:stuck_out_tongue: yep but its great for an old-timer like me who has a terrible memory, especially on linux where most stuff is shell commands. Its a excellent quick answer to some syntax or quick question.

double check it’s “answers”, which is why im posting here today.

Are you logged in to your DE as these commands are run? All these commands need your user environment loaded.

The old way with PulseAudio was to set up a system wide server, which was never the way you wanted it, and always a huge hassle.

(The selfish part of me posts on topics like this as I’m learning the ins and outs of PipeWire.)

You can run your backup service as root, and have it trigger another oneshot service which uses a User= directive to trigger a script that plays the sound.

Something like:

# /etc/systemd/system/backup-notify.service

[Unit]
Description=Play backup notification sound

[Service]
User=greg
Type=oneshot
ExecStart=/home/greg/.local/bin/backup-sound
# /home/greg/.local/bin/backup-sound

#!/usr/bin/bash

/usr/bin/play '/home/greg/sounds/winsounds/Windows Message Nudge.wav'

If your backup service runs a script add this to it:

/usr/bin/systemctl start backup-notify

or if you just have a service, add this to it:

ExecStart=/usr/bin/systemctl start backup-notify

Since the second service runs as your user it should work. :crossed_fingers:

2 Likes

Yep, definitely am logged into my desktop (DE).

Please no huge hassles :slight_smile:

Just to play a plink/dink sound! I can live without notify sound. Appreciate your time though. As i say just above "Please no huge hassles :slight_smile: "

It takes far less time and effort than you’ve spent asking here. :man_shrugging:

No worries, it was interesting and not much effort.

Yea, possibly. I’ve only just been forced to even look at systemd yesterday because I had to change all my NAS mounts over from fstab to systemd automounts. So any extra units are not as quick and easy as might be for you.

I posted to just ask if there was a single command i could use at the top of my script, but if not then no matter.

I may get bored in a couple of days and revisit this, though; it sounds like something I’d do.

Thx all.

All you have to do is copy it…not that it matters, your computer.

I had fun, and perhaps it’ll help someone else (or you in a few days). :smiley:

Cool :slight_smile:

@dmt
Have fun on your “break-through’s” and say hi to the jester from me ^^

the command works in my shell but if i put it into my backup script that is run via systemd it gives this error

pulseaudio / pipewire-pulse audio servers run in user space, so paplay command must also be run without root privileges

AFAIK there is no play command available on Manjaro

aplay is usually used to play audio to ALSA, but it can use a plug-in to play to default audio server (pulseaudio / pipewire-pulse)

/usr/bin/aplay -D default example.wav

pw-cat works with PipeWire only, so it would not work for OP If they are using pulseaudio

If pw-cat works for OP they can also use other PipeWire commands to debug audio issues
Arch manual pages - pipewire

What is wrong with system mode? – PulseAudio

In most desktop use cases, system mode likely is not the right choice

So why have it then?

System mode is around for usage on thin client or embedded setups, where no real local user exists, where access is exclusively via the network, and where state data is flushed on each session termination.

It’s provided by sox which is in extra.

1 Like