Skip to content

Instantly share code, notes, and snippets.

@koivunen
Last active July 28, 2017 08:20
Show Gist options
  • Save koivunen/67f2d5851037d39a19519d2362b5b56e to your computer and use it in GitHub Desktop.
Save koivunen/67f2d5851037d39a19519d2362b5b56e to your computer and use it in GitHub Desktop.
Kill user session after X11 inactivity, prototype version
[Unit]
Description=KILL_DISPLAY_SESSION_ON_IDLE
After=display-manager.service
[Service]
Environment=DISPLAY=:0
WorkingDirectory=/home/display/
Type=simple
ExecStart=/usr/sbin/idlekillr display
Restart=always
RestartSec=5
StandardOutput=syslog+console
TimeoutStartSec=5
StandardError=syslog
[Install]
WantedBy=display-manager.service
#!/usr/bin/env python3
import subprocess
from subprocess import call
import syslog
import time,os
import sys
assert sys.argv[1]
MAXT=os.getenv("KILL_TIME") or 10
DEVNULL=open("/dev/null",'wb')
def getIdleSec():
try:
ret = subprocess.check_output("xprintidle",stderr=DEVNULL)
ret = int(ret)/1000
return ret
except subprocess.CalledProcessError as e:
return
except ValueError as e:
return
def log(str):
print(str)
#syslog.syslog(str)
log("Running idlekillr")
def run():
while True:
prev = getIdleSec()
if prev and prev>1:
break
time.sleep(0.5)
initial=True
last=time.time()
log("started tracking")
while True:
time.sleep(2.3)
now=time.time()
cur = getIdleSec()
if not cur:
log("Lost screen, restarting")
break
if cur-prev<0: # triggered
initial=False
last=now
log("triggered")
prev=cur
if not initial and (now-last)>MAXT:
log("killtime")
call(["/usr/bin/killall","-3","-u",sys.argv[1]])
call(["/etc/init.d/nodm","stop"])
call(["/usr/bin/killall","-9","-u",sys.argv[1]])
call(["/etc/init.d/nodm","start"])
break
while True:
run()
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment