pid killer (ranges too)
import psutil
while True:
pids = input("Type the PID(s) you want to kill, separated by commas, or specify a range with a dash (-): ")
if pids.lower() == "exit" or pids.lower() == "stop":
confirm = input("Are you sure you want to stop the script? (Y/N): ")
if confirm.lower() in ["y", "yes"]:
break
else:
continue
if '-' in pids:
start, end = pids.split('-')
pids_list = [str(pid) for pid in range(int(start), int(end) + 1)]
yes_all = input("Do you want to kill all processes in the range without confirmation? (Y/N): ")
if yes_all.lower() in ['y', 'yes']:
response = 'y'
elif yes_all.lower() in ['n', 'no']:
response = ''
else:
response = ''
else:
pids_list = pids.split(",")
response = ''
for pid in pids_list:
try:
process = psutil.Process(int(pid))
name = process.name()
mem_usage = process.memory_info().rss / 1024 / 1024
cpu_usage = process.cpu_percent()
net_io_counters = psutil.net_io_counters(pernic=False)
network_usage = net_io_counters.bytes_sent / 1024 / 1024 + net_io_counters.bytes_recv / 1024 / 1024
disk_usage = process.io_counters().write_bytes / 1024 / 1024 + process.io_counters().read_bytes / 1024 / 1024
if response.lower() in ['y', 'yes']:
process.kill()
print(f"Process with PID {pid} ({name}) terminated.")
elif response.lower() in ['n', 'no']:
response = input(f"Y/N are you sure that you want to kill PID {pid} ({name}) current: mem {mem_usage:.2f}MB, CPU {cpu_usage:.2f}%, net {network_usage:.2f}MB, disk {disk_usage:.2f}MB? ")
if response.lower() in ['y', 'yes', 'yeah', 'yep', 'sure', 'ok', 'okay', 'fine', 'affirmative', 'positive']:
process.kill()
print(f"Process with PID {pid} ({name}) terminated.")
else:
print(f"Skipped terminating process with PID {pid} ({name}).")
else:
response = input(f"Y/N are you sure that you want to kill PID {pid} ({name}) current: mem {mem_usage:.2f}MB, CPU {cpu_usage:.2f}%, net {network_usage:.2f}MB, disk {disk_usage:.2f}MB? ")
if response.lower() in ['y', 'yes', 'yeah', 'yep', 'sure', 'ok', 'okay', 'fine', 'affirmative', 'positive']:
process.kill()
print(f"Process with PID {pid} ({name}) terminated.")
else:
print(f"Skipped terminating process with PID {pid} ({name}).")
except (psutil.NoSuchProcess, psutil.AccessDenied, ValueError) as e:
print(f"Error: Cannot kill process with PID {pid}. Reason: {e}")
No Comments