2018-10-06 16:49:06 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
import PySimpleGUI as sg
|
|
|
|
else:
|
|
|
|
import PySimpleGUI27 as sg
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import psutil
|
|
|
|
import operator
|
|
|
|
|
2019-06-12 15:23:47 +00:00
|
|
|
CONFIRM_KILLS = False
|
2018-10-06 16:49:06 +00:00
|
|
|
|
|
|
|
"""
|
2018-10-07 02:42:47 +00:00
|
|
|
Utility to show running processes, CPU usage and provides way to kill processes.
|
|
|
|
Based on psutil package that is easily installed using pip
|
2018-10-06 16:49:06 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
|
|
|
|
timeout=None, on_terminate=None):
|
|
|
|
"""Kill a process tree (including grandchildren) with signal
|
|
|
|
"sig" and return a (gone, still_alive) tuple.
|
|
|
|
"on_terminate", if specified, is a callabck function which is
|
|
|
|
called as soon as a child terminates.
|
|
|
|
"""
|
|
|
|
if pid == os.getpid():
|
|
|
|
raise RuntimeError("I refuse to kill myself")
|
|
|
|
parent = psutil.Process(pid)
|
|
|
|
children = parent.children(recursive=True)
|
|
|
|
if include_parent:
|
|
|
|
children.append(parent)
|
|
|
|
for p in children:
|
|
|
|
p.send_signal(sig)
|
|
|
|
gone, alive = psutil.wait_procs(children, timeout=timeout,
|
|
|
|
callback=on_terminate)
|
|
|
|
return (gone, alive)
|
|
|
|
|
|
|
|
|
2019-06-12 15:23:47 +00:00
|
|
|
def show_list_by_name(window):
|
|
|
|
psutil.cpu_percent(interval=.1)
|
|
|
|
procs = psutil.process_iter()
|
|
|
|
all_procs = [[proc.cpu_percent(), proc.name(), proc.pid] for proc in procs]
|
|
|
|
sorted_by_cpu_procs = sorted(all_procs, key=operator.itemgetter(1), reverse=False)
|
|
|
|
display_list = []
|
|
|
|
for process in sorted_by_cpu_procs:
|
|
|
|
display_list.append('{:5d} {:5.2f} {}\n'.format(process[2], process[0] / 10, process[1]))
|
|
|
|
window.FindElement('_processes_').Update(display_list)
|
|
|
|
return display_list
|
|
|
|
|
2018-10-06 16:49:06 +00:00
|
|
|
def main():
|
|
|
|
|
|
|
|
# ---------------- Create Form ----------------
|
2018-10-07 03:52:11 +00:00
|
|
|
# sg.ChangeLookAndFeel('Topanga')
|
|
|
|
|
2018-10-06 16:49:06 +00:00
|
|
|
layout = [[sg.Text('Process Killer - Choose one or more processes',
|
2018-10-07 18:19:27 +00:00
|
|
|
size=(45,1), font=('Helvetica', 15), text_color='red')],
|
2018-10-07 02:42:47 +00:00
|
|
|
[sg.Listbox(values=[' '], size=(50, 30), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 12), key='_processes_')],
|
2018-10-06 16:49:06 +00:00
|
|
|
[sg.Text('Click refresh once or twice.. once for list, second to get CPU usage')],
|
2018-10-07 02:42:47 +00:00
|
|
|
[sg.T('Filter by typing name', font='ANY 14'), sg.In(size=(15,1), font='any 14', key='_filter_')],
|
2018-10-29 00:01:03 +00:00
|
|
|
[sg.Button('Sort by Name', ),
|
|
|
|
sg.Button('Sort by % CPU', button_color=('white', 'DarkOrange2')),
|
|
|
|
sg.Button('Kill', button_color=('white','red'), bind_return_key=True),
|
2018-10-07 02:42:47 +00:00
|
|
|
sg.Exit(button_color=('white', 'sea green'))]]
|
2018-10-06 16:49:06 +00:00
|
|
|
|
|
|
|
window = sg.Window('Process Killer',
|
|
|
|
keep_on_top=True,
|
|
|
|
auto_size_buttons=False,
|
2018-10-07 18:18:52 +00:00
|
|
|
default_button_element_size=(12,1),
|
2018-10-07 02:42:47 +00:00
|
|
|
return_keyboard_events=True,
|
2019-06-12 15:23:47 +00:00
|
|
|
).Layout(layout).Finalize()
|
|
|
|
|
2018-10-06 16:49:06 +00:00
|
|
|
|
2019-06-12 15:23:47 +00:00
|
|
|
display_list = show_list_by_name(window)
|
2018-10-06 16:49:06 +00:00
|
|
|
# ---------------- main loop ----------------
|
|
|
|
while (True):
|
|
|
|
# --------- Read and update window --------
|
2018-10-15 20:07:23 +00:00
|
|
|
event, values = window.Read()
|
2018-10-29 00:01:03 +00:00
|
|
|
if event is None or event == 'Exit':
|
|
|
|
break
|
|
|
|
|
|
|
|
# skip mouse, control key and shift key events entirely
|
2018-10-15 20:07:23 +00:00
|
|
|
if 'Mouse' in event or 'Control' in event or 'Shift' in event:
|
2018-10-07 18:18:52 +00:00
|
|
|
continue
|
2018-10-06 16:49:06 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
# --------- Do Button Operations --------
|
2018-10-15 20:07:23 +00:00
|
|
|
if event == 'Sort by Name':
|
2019-06-12 15:23:47 +00:00
|
|
|
display_list = show_list_by_name(window)
|
|
|
|
# psutil.cpu_percent(interval=.1)
|
|
|
|
# procs = psutil.process_iter()
|
|
|
|
# all_procs = [[proc.cpu_percent(), proc.name(), proc.pid] for proc in procs]
|
|
|
|
# sorted_by_cpu_procs = sorted(all_procs, key=operator.itemgetter(1), reverse=False)
|
|
|
|
# display_list = []
|
|
|
|
# for process in sorted_by_cpu_procs:
|
|
|
|
# display_list.append('{:5d} {:5.2f} {}\n'.format(process[2], process[0]/10, process[1]))
|
|
|
|
# window.FindElement('_processes_').Update(display_list)
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Kill':
|
2018-10-06 16:49:06 +00:00
|
|
|
processes_to_kill = values['_processes_']
|
|
|
|
for proc in processes_to_kill:
|
|
|
|
pid = int(proc[0:5])
|
2019-06-12 15:23:47 +00:00
|
|
|
# if sg.PopupYesNo('About to kill {} {}'.format(pid, proc[12:]), keep_on_top=True) == 'Yes':
|
|
|
|
try:
|
|
|
|
kill_proc_tree(pid=pid)
|
|
|
|
except:
|
|
|
|
sg.PopupNoWait('Error killing process', auto_close_duration=1, auto_close=True)
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Sort by % CPU':
|
2019-06-12 15:23:47 +00:00
|
|
|
psutil.cpu_percent(interval=.1)
|
2018-10-07 18:18:52 +00:00
|
|
|
procs = psutil.process_iter()
|
|
|
|
all_procs = [[proc.cpu_percent(), proc.name(), proc.pid] for proc in procs]
|
|
|
|
sorted_by_cpu_procs = sorted(all_procs, key=operator.itemgetter(0), reverse=True)
|
|
|
|
display_list = []
|
|
|
|
for process in sorted_by_cpu_procs:
|
|
|
|
display_list.append('{:5d} {:5.2f} {}\n'.format(process[2], process[0]/10, process[1]))
|
|
|
|
window.FindElement('_processes_').Update(display_list)
|
2018-10-29 00:01:03 +00:00
|
|
|
else: # was a typed character
|
2018-10-07 18:18:52 +00:00
|
|
|
if display_list is not None:
|
|
|
|
new_output = []
|
|
|
|
for line in display_list:
|
|
|
|
if values['_filter_'] in line.lower():
|
|
|
|
new_output.append(line)
|
|
|
|
window.FindElement('_processes_').Update(new_output)
|
2018-10-07 02:42:47 +00:00
|
|
|
|
|
|
|
|
2018-10-06 16:49:06 +00:00
|
|
|
if __name__ == "__main__":
|
2018-10-15 20:07:23 +00:00
|
|
|
main()
|
|
|
|
sys.exit(0)
|