KonsoleKiller/killusall.py

35 lines
997 B
Python
Executable File

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import dbus
from os.path import basename
from xml.etree import ElementTree
IDLE_PROCESS = ["zsh", "bash", "fish"]
KONSOLE_PATH = 'org.kde.konsole'
def is_shell(pid):
with open('/proc/{pid}/cmdline'.format(pid=pid)) as file:
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(
session_bus.get_object(KONSOLE_PATH, '/Sessions').Introspect()
)
for x in id_list:
object_path = "/Sessions/{id}".format(id=x)
session = session_bus.get_object(KONSOLE_PATH, object_path)
foreground_id = session.foregroundProcessId()
if is_shell(foreground_id):
session.sendText("\4")