KonsoleKiller/killusall.py

35 lines
997 B
Python
Raw Normal View History

2013-10-18 19:00:23 +00:00
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import dbus
from os.path import basename
from xml.etree import ElementTree
2019-08-06 13:08:59 +00:00
IDLE_PROCESS = ["zsh", "bash", "fish"]
2013-10-18 19:00:23 +00:00
KONSOLE_PATH = 'org.kde.konsole'
2013-10-18 19:33:24 +00:00
def is_shell(pid):
with open('/proc/{pid}/cmdline'.format(pid=pid)) as file:
2013-10-18 19:00:23 +00:00
path = file.read()
process_name = basename(path)[:-1] # \x00 >:
return(process_name in IDLE_PROCESS)
def get_sessions_ids(sessions_xml):
root = ElementTree.fromstring(sessions_xml)
return [x.attrib['name'] for x in root if x.tag == 'node']
if __name__ == "__main__":
session_bus = dbus.SessionBus()
id_list = get_sessions_ids(
2013-10-18 19:22:43 +00:00
session_bus.get_object(KONSOLE_PATH, '/Sessions').Introspect()
2013-10-18 19:00:23 +00:00
)
for x in id_list:
object_path = "/Sessions/{id}".format(id=x)
2013-10-18 19:22:43 +00:00
session = session_bus.get_object(KONSOLE_PATH, object_path)
2013-10-18 19:00:23 +00:00
foreground_id = session.foregroundProcessId()
if is_shell(foreground_id):
2020-04-26 17:09:17 +00:00
session.sendText("\4")