Sleep(Suspend) When Idle

From MythTV Official Wiki
Revision as of 07:27, 14 September 2012 by Wagnerrp (talk | contribs) (category specifically for python scripts using the python bindings)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The script below can be used to put a frontend to sleep whenever xscreensaver turns the display 'Off' via DPMS. It was written with Mythbuntu 12.04/XFCE in mind, however it could probably be adapted for others distros.

Every 30 seconds it checks if mythfrontend is running and no additional users are logged in (via SSH), if so, it will check the DPMS status via xset, and if it is 'Off' it will suspend the machine.

Make the script executable by your frontend user account, and configure it to run in the session start.

NOTE: DPMS has 4 states; on, sleep, suspend, and off. The script will only trigger on the 'Off' state.

#!/usr/bin/env python
from subprocess import call, check_output
import time

# Mythtv frontend process name
fecommand = "mythfrontend.real"

# Command to use to suspend the computer
sleepcmd= ('dbus-send --system --print-reply '
           '--dest="org.freedesktop.UPower" '
           '/org/freedesktop/UPower '

# Start infinate loop
while True :

  # Wait 30 seconds before performing checks
  time.sleep (30)

  # Determine if the Frontend is stopped
  festopped = call("pidof " + fecommand + " >nul", shell=True)

  # Get the number of logged in users
  numusers = check_output("who -q | grep users | awk -F'=' '{print $2}'", shell=True)

  # If the number of logged in users <2 and the front end is not stopped
  if int(numusers) < 2 and not festopped:

    # Get the monitor DPMS status
    status = check_output('xset q |grep "Monitor is "', shell=True)

    # If the monitor DMPS status is 'Off'
    if 'Off' in status:

      # Run the sleep command
      call(sleepcmd, shell=True)