-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_killer.py
More file actions
68 lines (60 loc) · 2.44 KB
/
Copy pathtask_killer.py
File metadata and controls
68 lines (60 loc) · 2.44 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import wmi
import os
import sys
from constants import PROGRAMS_LIST
manager = wmi.WMI()
def discover_process(target_process: str):
for process in manager.Win32_Process():
if (process.Name == target_process):
print(f"Discovered {target_process}. running")
return process
print(f"Process name: {target_process} is not running")
raise ProcessLookupError()
def list_processes():
process_list = []
for process in manager.Win32_Process():
process_list.append(process.Name)
return process_list
def shutdown(process_name: str):
# 'vmmem' is a special case, as it is not owned by windows, so it must be shutdown with
# the 'wsl --shutdown command'
if (process_name == "vmmem"):
print("Issued shutdown command for vmmem process")
os.system('cmd /c "wsl --shutdown"')
else:
try:
running_process = discover_process(process_name)
running_process.Terminate()
except ProcessLookupError as err:
print(f"Process Not Found. May not be running: {err}")
except Exception as err:
print(f"Unknown error occurred. Details {err}")
raise err
def kill_if_programs_running(target_process_name: str,
comparable_processes: list,
running_processes: list):
for element in running_processes:
if element in comparable_processes:
print(f"Discovered task: {element}. Attempting to terminate process {target_process_name}")
shutdown(process_name=target_process_name)
return
print("Cannot find queried processes running. Target process will stay alive.")
# task run definition
if __name__ == "__main__":
try:
proc_name = sys.argv[1]
print(f"Target process name: {proc_name}")
proc_list = list_processes()
kill_if_programs_running(target_process_name=proc_name,
comparable_processes=PROGRAMS_LIST,
running_processes=proc_list)
exit(0)
except IndexError as err:
print(f"IndexError: ensure the process name is passed as launch argument: {err}")
exit(1)
except SystemExit as exit_req:
print(f"System exit due to successful program shutdown: {exit_req}")
raise exit_req
except Exception as err:
print(f"An unknown error has occurred: {err}")
exit(1)