Skip to content

Instantly share code, notes, and snippets.

@datenwolf
Created May 12, 2015 13:36
Show Gist options
  • Save datenwolf/c719e315a4f054d9206b to your computer and use it in GitHub Desktop.
Save datenwolf/c719e315a4f054d9206b to your computer and use it in GitHub Desktop.
Small Python script that locks all XScreenSaver instances running on a system – useful for suspend to … scripts
#!/usr/bin/python2
# vim: set fileencoding=utf-8 syntax=python tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab:
import os, os.path, psutil
def procenv(pid):
try:
env_fd = open(os.path.join('/proc', str(pid), 'environ'), 'r')
environ = env_fd.read()
env_fd.close()
return {e[0]:e[2] for e in [e.partition('=') for e in environ.split('\0')]}
except:
pass
return {}
if __name__ == '__main__':
for proc in [p for p in psutil.process_iter() if 'xscreensaver' == p.name()]:
env = procenv(proc.pid)
if not 'DISPLAY' in env.keys():
continue
display = env['DISPLAY']
xauthority = env['XAUTHORITY'] if 'XAUTHORITY' in env.keys() else os.path.join(env['HOME'], '.Xauthority')
pid = os.fork()
if 0 == pid:
exe = 'xscreensaver-command'
os.execvpe(exe, [exe, '--lock'], {'DISPLAY':display, 'XAUTHORITY':xauthority})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment