You are not logged in.

#1 2019-05-08 14:30:32

alfalfa
Member
Registered: 2017-06-30
Posts: 63
Website

[SOLVED] Play a sound before suspend

On my laptop I often use systemd-inhibit to block suspend when the lid is closed. However, sometime I forget that the inhibit is running, and then my battery drain itself before I notice it. To fix this, I would like to play a sound before going to sleep. I tried several ways but none worked so far. Here is what I've tried:

1. Listening to logind DBus signal with a python script: too slow and the sound is only played on resume
2. Put a script in /usr/lib/systemd/system-sleep/suspendsound.sh:

#!/bin/bash
export DISPLAY=:0
export XAUTHORITY=/home/user/.Xauthority
aplay /home/user/scripts/coin.wav

However that did not work because systemd cannot use pulseaudio, so I tried to assign a non-pulse output with aplay -D. In aplay -L the only revelant output are front, default and pulse, but only default and pulse worked (both are the same).

3. Create a systemd service (/etc/systemd/system/suspend@user.service):

[Unit]
Description=Play sound before suspend
Before=sleep.target

[Service]
User=user
Type=oneshot
Environment=DISPLAY=:0
ExecStart=/usr/bin/aplay -D default /home/user/scripts/coin.wav
RemainAfterExit=true

[Install]
WantedBy=sleep.target

But the outcome is similar to #2:

May 08 00:44:41 peppy systemd[1]: Failed to start User suspend actions.
May 08 00:44:41 peppy systemd[1]: suspend@user.service: Failed with result 'exit-code'.
May 08 00:44:41 peppy systemd[1]: suspend@user.service: Control process exited, code=exited, status=1/FAILURE
May 08 00:44:41 peppy aplay[25493]: aplay: main:828: audio open error: No such file or directory

4. Create a systemd service on the user side (/etc/systemd/user/suspend@user.service):
I used the service from #3, but without the assigning User=. It plays when I start the service in the console, but it is not triggered on suspend.

---

The last resort would be to set HandleLidSwitch=ignore in logind.conf, and use a script to listen to ACPI events so it play a sound and then call suspend.
Surely there must be a better way for such a simple task?

Last edited by alfalfa (2019-05-08 20:26:15)

Offline

#2 2019-05-08 14:36:31

seth
Member
Registered: 2012-09-03
Posts: 63,061

Re: [SOLVED] Play a sound before suspend

None of that is gonna work because of PA, see https://wiki.archlinux.org/index.php/Pu … ice,_cron)

Online

#3 2019-05-08 15:50:00

alfalfa
Member
Registered: 2017-06-30
Posts: 63
Website

Re: [SOLVED] Play a sound before suspend

seth wrote:

None of that is gonna work because of PA, see https://wiki.archlinux.org/index.php/Pu … ice,_cron)

I tried both solution in the link you mentionned:
1. /usr/lib/systemd/system-sleep/suspendsound.sh

#!/bin/bash
export DISPLAY=:0
export XAUTHORITY=/home/user/.Xauthority
export XDG_RUNTIME_DIR=/run/user/user
paplay /home/user/scripts/coin.oga

Output:

May 08 10:41:47 peppy [20040]: /usr/lib/systemd/system-sleep/suspendsound.sh failed with exit status 1.
May 08 10:41:47 peppy systemd-sleep[20005]: pa_context_connect() failed: Connection refused
May 08 10:41:47 peppy systemd-sleep[20005]: Connection failure: Connection refused

2. /usr/lib/systemd/system-sleep/suspendsound.sh

#!/bin/bash
machinectl shell .host --uid=user /usr/bin/paplay /home/user/scripts/coin.oga

Output:

May 08 11:39:37 peppy [28652]: /usr/lib/systemd/system-sleep/suspendsound.sh failed with exit status 1.
May 08 11:39:37 peppy systemd-sleep[28629]: Failed to get shell PTY: Specified path '--uid=user' is not absolute

Offline

#4 2019-05-08 15:54:53

zzzardoz
Member
Registered: 2017-12-30
Posts: 23

Re: [SOLVED] Play a sound before suspend

Wiki wrote:

(replace user_id with the ID of the user running PulseAudio)

Offline

#5 2019-05-08 15:57:31

alfalfa
Member
Registered: 2017-06-30
Posts: 63
Website

Re: [SOLVED] Play a sound before suspend

zzzardoz wrote:
Wiki wrote:

(replace user_id with the ID of the user running PulseAudio)

My username is "user"

Offline

#6 2019-05-08 17:14:34

zzzardoz
Member
Registered: 2017-12-30
Posts: 23

Re: [SOLVED] Play a sound before suspend

username ≠ user_id !

id -u <username>

Offline

#7 2019-05-08 17:43:02

alfalfa
Member
Registered: 2017-06-30
Posts: 63
Website

Re: [SOLVED] Play a sound before suspend

zzzardoz wrote:

username ≠ user_id !

id -u <username>

Thanks for the hint smile

However, it still output an error in journalctl:

systemd-sleep[7719]: XDG_RUNTIME_DIR (/run/user/1000) is not owned by us (uid 0), but by uid 1000! (This could e g happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.) 

Offline

#8 2019-05-08 18:21:52

seth
Member
Registered: 2012-09-03
Posts: 63,061

Re: [SOLVED] Play a sound before suspend

The scripts run as root, use "sudo -u user …"

Online

#9 2019-05-08 19:17:30

alfalfa
Member
Registered: 2017-06-30
Posts: 63
Website

Re: [SOLVED] Play a sound before suspend

seth wrote:

The scripts run as root, use "sudo -u user …"

With paplay:

[user@peppy ~]$ sudo -u user /usr/lib/systemd/system-sleep/suspendsound.sh
Connection failure: Connection refused

With aplay:

sudo -u user /usr/lib/systemd/system-sleep/suspendsound.sh
ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave
aplay: main:828: audio open error: No such file or directory
Connection failure: Connection refused

Offline

#10 2019-05-08 19:26:31

seth
Member
Registered: 2012-09-03
Posts: 63,061

Re: [SOLVED] Play a sound before suspend

This is w/ XDG_RUNTIME_DIR=/run/user/1000 ?
THe only thing I could imagine is that PA is already suspended at that time (no idea, I don't and won't use PA) - you could still use https://www.archlinux.org/packages/extra/x86_64/beep/ (requires the pcspkr module loaded)

Online

#11 2019-05-08 20:17:37

alfalfa
Member
Registered: 2017-06-30
Posts: 63
Website

Re: [SOLVED] Play a sound before suspend

Edit: It works! Thank you!

/etc/systemd/system/suspend@user.service

[Unit]
Description=Play sound before suspend
Before=sleep.target

[Service]
Type=oneshot
ExecStart=/usr/bin/sudo -u user /usr/bin/play-sound-before-suspend

[Install]
WantedBy=sleep.target

/usr/bin/play-sound-before-suspend

#!/bin/bash
export XDG_RUNTIME_DIR=/run/user/1000
aplay /home/user/scripts/coin.wav

Last edited by alfalfa (2019-05-08 20:36:16)

Offline

#12 2019-05-08 20:25:31

seth
Member
Registered: 2012-09-03
Posts: 63,061

Online

Board footer

Powered by FluxBB