I wrote the following script to check for idle and update the session idle status with systemd-logind accordingly. (Normally this would be done by the session manager, but I don't run one.) This lets systemd put the system to sleep automatically (based on IdleAction
and IdleActionSec
in /etc/systemd/logind.conf
), with consideration of both X11 and ssh logins when determining whether the system is idle. Based partly on this and this.
You could modify the login1_idle class to run whatever scripts you want.
#!/usr/bin/python
import ctypes
import os
import dbus
import time
# Timing parameters, in s
idle_threshold = 60 # Amount of idle time required to consider things truly idle
check_interval = 50 # How often to check the X server idle indication
class login1_idle:
def __init__(self):
self.flag = 0
bus = dbus.SystemBus()
seat = bus.get_object('org.freedesktop.login1',
'/org/freedesktop/login1/seat/'
+ os.environ['XDG_SEAT'])
active_session = seat.Get('org.freedesktop.login1.Seat',
'ActiveSession',
dbus_interface='org.freedesktop.DBus.Properties')
session_obj_path = active_session[1]
self.session = bus.get_object('org.freedesktop.login1', session_obj_path);
def on_busy(self):
self.flag = 0
self.session.SetIdleHint(False,
dbus_interface='org.freedesktop.login1.Session')
def on_idle(self):
self.flag = 1
self.session.SetIdleHint(True,
dbus_interface='org.freedesktop.login1.Session')
class XScreenSaverInfo(ctypes.Structure):
""" typedef struct { ... } XScreenSaverInfo; """
_fields_ = [('window', ctypes.c_ulong), # screen saver window
('state', ctypes.c_int), # off,on,disabled
('kind', ctypes.c_int), # blanked,internal,external
('since', ctypes.c_ulong), # milliseconds
('idle', ctypes.c_ulong), # milliseconds
('event_mask', ctypes.c_ulong)] # events
xlib = ctypes.cdll.LoadLibrary('libX11.so')
display = xlib.XOpenDisplay(os.environ['DISPLAY'])
xss = ctypes.cdll.LoadLibrary('libXss.so.1')
xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
xssinfo = xss.XScreenSaverAllocInfo()
idle = login1_idle()
while True:
xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), xssinfo)
#print "idle: %d ms" % xssinfo.contents.idle
if xssinfo.contents.idle > idle_threshold * 1000 and not idle.flag:
print time.strftime('%c') + " doing on_idle"
idle.on_idle()
elif xssinfo.contents.idle < idle_threshold * 1000 and idle.flag:
print time.strftime('%c') + " doing on_busy"
idle.on_busy()
#print idle.session.Get('org.freedesktop.login1.Session',
# 'IdleHint',
# dbus_interface='org.freedesktop.DBus.Properties')
time.sleep(check_interval)
Another similar question with a C/bash implementation is Run a command when system is idle and when is active again.
xautolock
– number5 Nov 14 '13 at 01:35