forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill.py
51 lines (42 loc) · 1.85 KB
/
kill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""Kill processes.
Unix 'kill' wrapper extension.
Synopsis: <trigger> <filter>"""
import os
from signal import SIGKILL, SIGTERM
from albertv0 import FuncAction, Item, iconLookup
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Kill Process"
__version__ = "1.4"
__trigger__ = "kill "
__author__ = "Benedict Dudel, Manuel Schneider"
__dependencies__ = []
iconPath = iconLookup('process-stop')
def handleQuery(query):
if query.isTriggered:
results = []
uid = os.getuid()
for dir_entry in os.scandir('/proc'):
try:
if dir_entry.name.isdigit() and dir_entry.stat().st_uid == uid:
proc_command = open(os.path.join(dir_entry.path, 'comm'), 'r').read().strip()
if query.string in proc_command:
proc_cmdline = open(os.path.join(dir_entry.path, 'cmdline'), 'r').read().strip().replace("\0", " ")
results.append(
Item(
id="kill_%s" % proc_cmdline,
icon=iconPath,
text=proc_command.replace(query.string, "<u>%s</u>" % query.string),
subtext=proc_cmdline,
completion=query.rawString,
actions=[
FuncAction("Terminate", lambda pid=int(dir_entry.name): os.kill(pid, SIGTERM)),
FuncAction("Kill", lambda pid=int(dir_entry.name): os.kill(pid, SIGKILL))
]
)
)
except FileNotFoundError: # TOCTOU dirs may disappear
continue
except IOError: # TOCTOU dirs may disappear
continue
return results