How to avoid suspending while music is playing
I have a simple problem: I would like my computer to avoid suspending when music or another media is playing on a desktop application on my computer. I’m running Fedora Workstation 36 with Gnome 42.2.
This is my first time using GNU/Linux so I’m not very good at it. I did some research and was able to create a bash script with a command of pulseaudio/pipewire/WirePlumber:
command=$(pactl list sinks)
if [[ “$command” == *”$state”* ]]; then
echo “Media playing detected”
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type ‘nothing’
echo ‘Set suspend status OFF’
# sleep 10s
echo “Media playing no detected”
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type ‘suspend’
echo ‘Set suspend status ON’
time_suspend_sec=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout)
echo ‘Automatic suspend when idle’ $time_suspend_min ‘min’
# sleep 10s
The script works fine when I run it manually from the console, but I’m trying to get it to run automatically when I pause or resume playback. Maybe there is a WirePlumber API that sends a signal every time the playback state changes between play and pause, but I haven’t found it. I also thought to get this signal from the notification but I don’t know how to do it.
Another option would be to infinitely execute the script every second but I don’t know if this is possible.
Caffeine or Espresso Gnome extensions aren’t an option because they do not detect when a song is playing or paused, only when an application is running.
My desired behaviour is beneficial for several reasons:
If the user leaves a music application open in the background, but with playback halted, the computer will not stay awake indefinitely.
If the user leaves the computer with music playing, intending the music to carry on, they need not worry about having their music player in fullscreen to keep the music going.
The user can take advantage of this behaviour in different ways. For example, they could select 20 minutes of relaxing music before bed. When the 20 minute playlist ends, the computer will then suspend for the night. The sleep duration thus automatically adjusts to the length of the playlist.