-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessManager.py
More file actions
56 lines (46 loc) · 1.55 KB
/
ProcessManager.py
File metadata and controls
56 lines (46 loc) · 1.55 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
import psutil
import time
import subprocess
def kill_Process():
killed = 0
answer = input("Type in the program you want to kill: ").lower()
for p in psutil.process_iter(['pid', 'name']):
if p.info['name'] and answer in p.info['name'].lower():
try:
print(f"Killing {p.info['name']} with PID {p.info['pid']}")
time.sleep(0.5) # Optional delay for clarity
p.terminate() # termination
p.wait(timeout=3) # Wait to ensure the process exits
killed += 1
except psutil.NoSuchProcess:
print(f"Process {p.info['pid']} already exited.")
except psutil.AccessDenied:
print(f"No permission to terminate {p.info['pid']}.")
except Exception as e:
print(f"Unexpected error: {e}")
if killed == 0:
print(f"No processes found matching {answer}")
else:
print(f"Done! Killed {killed} processes.")
time.sleep(0.5)
def search_Process():
for p in psutil.process_iter(['pid', 'name']):
print(p.info)
print("Done!")
time.sleep(0.5)
while True:
print("Welcome to Process Management")
time.sleep(0.5)
user_choice = input("Select an option (1 - View Processes) or (2 - Kill a Process): ")
if user_choice == "1":
time.sleep(1)
search_Process()
break
elif user_choice == "2":
time.sleep(1)
kill_Process()
break
else:
time.sleep(1)
print("An error happened")
break