You are not logged in.
Pages: 1
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
None of that is gonna work because of PA, see https://wiki.archlinux.org/index.php/Pu … ice,_cron)
Online
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
(replace user_id with the ID of the user running PulseAudio)
Offline
Wiki wrote:(replace user_id with the ID of the user running PulseAudio)
My username is "user"
Offline
username ≠ user_id !
id -u <username>
Offline
username ≠ user_id !
id -u <username>
Thanks for the hint
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
The scripts run as root, use "sudo -u user …"
Online
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
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
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
Did you try https://wiki.archlinux.org/index.php/Po … stem-sleep ?
Online
Pages: 1