2

Because of this issue Intermittent - Dell XPS 9310, 22.04 does not turn off with lid closed (the lid switch doesn't trigger) I'd like to get feedback when sleep is initiated.

Is it possible to hook into the lid switch and play a notification sound when the lid is being closed and/or when sleep is triggered?

Edit: I've provided a partial answer below. However, the sound playback does not work. Is anybody else able to provide an answer including the sound playback?

bjohas
  • 513
  • 1
    For the sleep case, see this Q&A: https://askubuntu.com/questions/1313479/correct-way-to-execute-a-script-on-resume-from-suspend Short version: write a script in /lib/systemd/system-sleep/. – Jos May 09 '22 at 09:47
  • Thank you very much! – bjohas May 10 '22 at 18:44

1 Answers1

1

Thanks to Jos and the answer by Tracy here: Correct way to execute a script on resume from suspend I got part way. Then I found https://bbs.archlinux.org/viewtopic.php?id=246264.

Full solution: First make a note of your user name (say sam) and your user id:

id -u sam

Suppose your user id is 1000. On Ubuntu 22.04 LTS, place a file with this content into /lib/systemd/system-sleep/ (using your user name sam and id 1000):

#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin

case "$1" in pre) #code execution BEFORE sleeping/hibernating/suspending /usr/bin/sudo -u sam bash -c "export XDG_RUNTIME_DIR=/run/user/1000; /usr/bin/ogg123 -q /usr/share/sounds/Yaru/stereo/desktop-logoff.oga" /usr/bin/date >> SOMEDIR/logs/sleep-monitor/pre.log ;; post) #code execution AFTER resuming # same code as above would work here. ;; esac

exit 0

Check:

  • Log and playback should work is called manually (sudo script.sh pre).
  • The script should work when the system suspends, in that the log file pre.log is updated as expected. If that doesn't happen, check about the suspend service (see links above):
sudo systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target

Final note: Depending on your setup etc etc, it might take a bit for the laptop to actually suspend. So you'll only hear the sound once the laptop lid has been shut for a while.

bjohas
  • 513
  • Same unanswered question here: https://unix.stackexchange.com/questions/140882/play-sound-when-suspending-and-resuming – bjohas May 10 '22 at 22:08