Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Safe Mode for Termination and make it cross platform working #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
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

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:
Expand All @@ -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.")