As the title suggests, I wanted something very simple: Kubuntu / KDE Plasma shouldn't sleep while audio is playing. There are apparently script workarounds for Mint and Ubuntu using Gnome, but I couldn't find anything for KDE. Specifically, my need is this: A) If audio is playing, then don't sleep; B) but do please save power by turning the screen off; and C) allow the screen to lock for security reasons. My solution after reading the following references is below, but it's not exactly perfect (also, while my instructions are for Kubuntu, I assume some tweaks would make this solution suitable for any of the _ubuntus and probably other distros). I discuss this solution and ask questions at the end. Also, I apologize if I am posting in the wrong place or have broken some rule here; I hope this thread helps other users with my specific needs.
Reference:
https://ubuntuforums.org/showthread.php?t=2232064
https://unix.stackexchange.com/quest...rently-playing
https://stackoverflow.com/questions/...04952#17404952
https://bbs.archlinux.org/viewtopic.php?id=204253
Solution:
Note: Of course you can change the time variables to anything you want, just make sure to stick to the ordering: t 'Dim Screen' < t 'Screen Energy Saving' < t 'Lock Screen' < t 'sleep' (in script) < t 'Suspend session'. Also, make sure the 'Suspend session' under the 'On low battery' tab is less than t 'sleep', else your computer will drain completely.
Discussion:
First, what the script does: Basically, "pacmd list-sink-inputs | grep -c 'state: RUNNING'" checks if PulseAudio is playing, and the "xdotool mousemove_relative" lines tell xdotool to move the mouse cursor once every 11 minutes. What happens is the screen dims, shuts off, and then locks as usual, but while audio is playing xdotool moves the mouse cursor every 11 minutes so that screen turns on, which prevents the computer from sleeping. It's not perfect -- the screen turns on every 11 minutes and then dims and shuts off again -- but it's good enough. If someone knows how I could satisfy the above needs with a script without the screen turning on again, I'm all ears.
Second, I'm a long-time linux user, familiar primarily with Kubuntu and Manjaro with KDE. I'm not exactly a newbie, though I still don't have particular interest in leaving the GUI behind (hence my choice of KDE), and for circumstantial reasons I'm only able to improve my knowledge of Gnu/Linux by increments. More or less, through experience I know how not to mess up my system and how to Google solutions to most of my problems, and when the time comes opening the terminal, making a script, etc. are no big deal. Simply put: My knowledge of Gnu/Linux and KDE is good enough for my general needs, but not comprehensive (something I hope to find the time to work on).
That said, I was surprised I couldn't find a better solution than the above, which I think may be daunting for newbies (I hope I was detailed enough). Is there an out-of-the-box or otherwise click-and-fix solution that I am just missing? If not, I would love to hear thoughts as to why something like this isn't just standard. Most other solutions I found rely on manually starting something or other, then manually turning it off when the audio is finished playing, which feels to me to defeat the purpose of having a desktop environment such as KDE, or indeed a computer. I'm very willing to accept that I have just missed something, or that my setup is peculiar and therefore buggy, but if that's not the case, then I wonder why such a feature is not simply a standard? To me, having a computer sleep while playing audio is just plain odd.
Thanks in advance for your input!
Reference:
https://ubuntuforums.org/showthread.php?t=2232064
https://unix.stackexchange.com/quest...rently-playing
https://stackoverflow.com/questions/...04952#17404952
https://bbs.archlinux.org/viewtopic.php?id=204253
Solution:
- Install xdotool: sudo apt-get install xdotool
- Create script: Copy and paste code below into text file >> save as name_this_script.sh wherever you keep your scripts.
- Make script executable: Righ-click > Properties > Permissions >> Check 'Is Executable')
- Change 'Lock Screen' setting to be LESS THAN the 'sleep 11m' (see code below): System Settings >> Workspace Behavior >> Screen Locking >> Tick 'Automatically Lock Screen after [(t<11) minutes]
- Change 'Dim Screen' and 'Screen Energy Saving' settings to be LESS THAN the 'Lock Screen': System Settings >> Power Management >> Energy Saver > On AC Power/On Battery >> Tick 'Dim screen After [(t<10) minutes]' and 'Screen Energy Saving Switch off after [(t<9) minutes]'
- Change 'Suspend session' to be MORE THAN 'sleep 11m': System Settings > Power Management >> Energy Saver >> On AC Power/On Battery/On Low Battery > Tick 'Suspend session After [(t>11) minutes]'
- Run script on startup: System Settings >> Startup and Shutdown >> Autostart >> Add Script
Note: Of course you can change the time variables to anything you want, just make sure to stick to the ordering: t 'Dim Screen' < t 'Screen Energy Saving' < t 'Lock Screen' < t 'sleep' (in script) < t 'Suspend session'. Also, make sure the 'Suspend session' under the 'On low battery' tab is less than t 'sleep', else your computer will drain completely.
Code:
#! /bin/bash State=1 while [ State != 0 ]; do if pacmd list-sink-inputs | grep -c 'state: RUNNING'; then let State=1 #echo "Playing" xdotool mousemove_relative -- -1 -1 sleep 11m xdotool mousemove_relative 1 1 sleep 11m else let State=0 #echo "Idle" fi done
First, what the script does: Basically, "pacmd list-sink-inputs | grep -c 'state: RUNNING'" checks if PulseAudio is playing, and the "xdotool mousemove_relative" lines tell xdotool to move the mouse cursor once every 11 minutes. What happens is the screen dims, shuts off, and then locks as usual, but while audio is playing xdotool moves the mouse cursor every 11 minutes so that screen turns on, which prevents the computer from sleeping. It's not perfect -- the screen turns on every 11 minutes and then dims and shuts off again -- but it's good enough. If someone knows how I could satisfy the above needs with a script without the screen turning on again, I'm all ears.
Second, I'm a long-time linux user, familiar primarily with Kubuntu and Manjaro with KDE. I'm not exactly a newbie, though I still don't have particular interest in leaving the GUI behind (hence my choice of KDE), and for circumstantial reasons I'm only able to improve my knowledge of Gnu/Linux by increments. More or less, through experience I know how not to mess up my system and how to Google solutions to most of my problems, and when the time comes opening the terminal, making a script, etc. are no big deal. Simply put: My knowledge of Gnu/Linux and KDE is good enough for my general needs, but not comprehensive (something I hope to find the time to work on).
That said, I was surprised I couldn't find a better solution than the above, which I think may be daunting for newbies (I hope I was detailed enough). Is there an out-of-the-box or otherwise click-and-fix solution that I am just missing? If not, I would love to hear thoughts as to why something like this isn't just standard. Most other solutions I found rely on manually starting something or other, then manually turning it off when the audio is finished playing, which feels to me to defeat the purpose of having a desktop environment such as KDE, or indeed a computer. I'm very willing to accept that I have just missed something, or that my setup is peculiar and therefore buggy, but if that's not the case, then I wonder why such a feature is not simply a standard? To me, having a computer sleep while playing audio is just plain odd.
Thanks in advance for your input!
Comment