qutewald.py 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. import os
  3. import signal
  4. import sys
  5. from pathlib import Path
  6. import daemon
  7. import psutil
  8. from daemon.pidfile import PIDLockFile
  9. from inotify_simple import INotify, flags
  10. def cleanup():
  11. os.remove(pidfile)
  12. sys.exit()
  13. pidfile = '/tmp/iqutefy.pid'
  14. # check if pidfile refers to dead process
  15. # if so, remove it
  16. if os.path.isfile(pidfile):
  17. with open(pidfile) as pf:
  18. pid = int(pf.readline())
  19. if not psutil.pid_exists(pid):
  20. os.remove(pidfile)
  21. with daemon.DaemonContext(
  22. detach_process=False,
  23. pidfile=PIDLockFile(pidfile),
  24. signal_map={signal.SIGTERM: cleanup}):
  25. inotify = INotify()
  26. watch_flags = flags.CREATE | flags.MODIFY
  27. wd = inotify.add_watch(sys.argv[1], watch_flags)
  28. process = psutil.Process()
  29. # inotify iterator runs out immediately
  30. while True:
  31. for event in inotify.read():
  32. os.system('qutebrowser :config-source')
  33. # kill after qutebrowser exits
  34. # not reached on SIGKILL
  35. cleanup()