INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Simple Script to Keep the System Awake While Media is Playing on Linux  (Read 2234 times)

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5234
  • "Linux Merit Badge" Recipient

So I'd been struggling for some time with the following issue:  JRiver for Linux did not reliably prevent the system from going to sleep while playing media (as it does on windows).  I initially thought this was a Gnome issue (because its wakelock API is broken), but I had the identical problem with a default debian XFCE4 install as well.  So I went for a while just setting system sleep to a long value (i.e. idle for three hours, then turn off), but that was unsatisfactory for all kinds of reasons.  So I wrote the following script, and it works for my needs.  I'm improving it, but thought I'd offer the basic version as this will work on a default debian install (which is the supported environment for MC).  But it has some caveats:

1) Right now it only works on X11 (I'm trying to work out how I can produce similar results on Wayland systems).  This will mostly affect Gnome users, which is wayland by default, but you can choose an X11 session of Gnome if you want to use this script.
2) If you plan to use it with cron you'll need to make some changes to your sudoers file with visudo, as described in the script documentation.

The script's comments explain how to use it.

Code: [Select]
#!/bin/bash
#
# A script to suspend based on both user idleness and audio output
# (i.e. so your system stops suspending during movies, etc.).
#
# This is intended to replace your DE's power management functions, so
# turn them off, and then put this in your crontab running every few
# minutes, or make a systemd timer/service that runs it.  It has been
# tested with cron and works provided you follow these instructions:
#
# 1) Verify that the DISPLAY variable below matches your user's env
# (or the user whose running the interactive session), and
#
# 2) Install xprintidle if it's not already installed (it's in the
# repos for almost every distro.
#
# 3) You'll need to add your user to the sudoers file for passwordless
# use of systemctl suspend due to some goofy issues where users can't
# suspend without authentication if other users are logged in, but
# cron's shell is counted as another user despite just being another
# instance of your user. Run "which systemctl" to find out the path to
# systemctl on your system, then run visudo as root, and add a line
# like the following:
#
# your-user-name ALL=NOPASSWD: /bin/systemctl suspend
#
# If "which systemctl" shows something other than /bin/systemctl, use
# that as the path instead
#

#This is necessary for xprintidle to work from cron
export DISPLAY=:0.0
#This finds time in milliseconds that the user hasn't touched the mouse or
#keyboard
IDLE=$(/usr/bin/xprintidle)
#If the user has been idle longer than ten minutes and no audio is
#playing suspend the system
if [ "$IDLE" -gt 600000 ]
then
    grep -q RUNNING /proc/asound/card*/pcm*/sub*/status || sudo systemctl suspend
fi


It's fairly "dumb", but it's been working perfectly for me.
Logged
Pages: [1]   Go Up