From ae2943943fbfec5ecad1f12e6ac0016c0d47b487 Mon Sep 17 00:00:00 2001 From: codebuddyjr Date: Sat, 19 Oct 2024 00:14:16 +0530 Subject: [PATCH] Added a Safe Mode for Termination and make it cross platform working --- main.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index d0a0208..58f71c3 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,27 @@ import psutil -import time -import threading -import urwid import os def list_processes(): print(f"\033[92;42m{'PID':<10}{'Name':<50}{'CPU Usage (%)':<15}{'Memory Usage (in MB)':<15}\033[0m") print("-" * 91) - system_paths = ['C:\\Windows\\System32', 'C:\\Windows\\'] + # Handle system paths for both Windows and Linux + if os.name == 'nt': # Windows + system_paths = ['C:\\Windows\\System32', 'C:\\Windows\\'] + else: # Linux/Unix-like + system_paths = ['/usr/sbin', '/sbin', '/bin', '/usr/bin'] current_user = psutil.Process().username() - for process in psutil.process_iter(["pid", "name", "cpu_percent", "memory_percent", "exe", "username", "status"]): + for process in psutil.process_iter(["pid", "name", "cpu_percent", "memory_info", "exe", "username", "status"]): try: name = process.info["name"] exe_path = process.info["exe"] if process.info["exe"] else "" username = process.info.get("username", "") if process.info["status"] == psutil.STATUS_RUNNING and username == current_user and not any(exe_path.startswith(path) for path in system_paths): - print(f"\033[94m{process.info['pid']:<10}\033[0m\033[95m{process.info['name']:<50}\033[0m{process.info['cpu_percent']:<15}\033[96m{(process.info['memory_percent']*psutil.virtual_memory()[0])/10**8:<15} MB\033[0m") + # Calculate memory usage in MB + memory_usage_mb = process.memory_info().rss / (1024 * 1024) + print(f"\033[94m{process.info['pid']:<10}\033[0m\033[95m{name:<50}\033[0m{process.info['cpu_percent']:<15}\033[96m{memory_usage_mb:<15.2f} MB\033[0m") except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): continue @@ -26,10 +29,17 @@ def list_processes(): def kill_process(pid): try: process = psutil.Process(pid) - print(f"Terminating process {pid} ({process.name()})..."); + + # Prevent termination of crucial system processes + if pid == 1 or process.name() in ['systemd', 'init', 'sshd']: + print(f"Error: Cannot terminate crucial system process {process.name()} (PID {pid}).") + return + + print(f"Terminating process {pid} ({process.name()})...") process.terminate() process.wait(timeout=3) print(f"Process {pid} ({process.name()}) has been terminated.") + except psutil.NoSuchProcess: print(f"Process with PID {pid} does not exist.") except psutil.AccessDenied: @@ -42,6 +52,6 @@ def kill_process(pid): kill_pid = int(input("Enter the PID of the process you want to terminate: ")) kill_process(kill_pid) except KeyboardInterrupt: - print("\nOops! There is some interruption, this is awkward XD.") + print("\nOops! There was some interruption; this is awkward XD.") except ValueError: print("Invalid PID entered. Please enter a valid PID.")