2025-07-31 14:04:19 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# Description: Displays top system processes with color.
|
|
|
|
|
# Usage: python3 color-top.py
|
|
|
|
|
# Author: Justin Oros
|
|
|
|
|
# Source: https://github.com/JustinOros
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import psutil
|
|
|
|
|
import sys
|
|
|
|
|
import select
|
|
|
|
|
import socket
|
|
|
|
|
import argparse
|
2025-07-31 14:08:27 -07:00
|
|
|
import tty
|
|
|
|
|
import termios
|
2025-07-31 14:04:19 -07:00
|
|
|
|
|
|
|
|
GREEN = "\033[32m"
|
|
|
|
|
YELLOW = "\033[33m"
|
|
|
|
|
ORANGE = "\033[38;5;214m"
|
|
|
|
|
RED = "\033[31m"
|
2025-07-31 14:27:49 -07:00
|
|
|
RESET_TO_GREEN = "\033[32m"
|
2025-07-31 14:04:19 -07:00
|
|
|
|
2025-07-31 14:08:27 -07:00
|
|
|
def clear_screen():
|
|
|
|
|
os.system('cls' if os.name == 'nt' else 'clear')
|
|
|
|
|
|
2025-07-31 14:04:19 -07:00
|
|
|
def get_top_processes(limit):
|
2025-07-31 14:08:27 -07:00
|
|
|
for proc in psutil.process_iter(['pid']):
|
|
|
|
|
try:
|
|
|
|
|
proc.cpu_percent(interval=None)
|
|
|
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
2025-07-31 14:04:19 -07:00
|
|
|
processes = []
|
|
|
|
|
for proc in psutil.process_iter(['cpu_percent', 'name', 'username']):
|
|
|
|
|
try:
|
|
|
|
|
cpu = proc.info['cpu_percent']
|
|
|
|
|
if cpu is not None:
|
|
|
|
|
processes.append((cpu, proc.info['name'], proc.info['username']))
|
|
|
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
|
|
|
continue
|
|
|
|
|
processes.sort(reverse=True, key=lambda p: p[0])
|
|
|
|
|
return processes[:limit]
|
|
|
|
|
|
|
|
|
|
def colorize(cpu, name, user):
|
2025-07-31 14:27:49 -07:00
|
|
|
if cpu <= 25:
|
|
|
|
|
return f"{cpu:6.2f}% {name[:25]:<25} {user[:15]:<15}"
|
|
|
|
|
else:
|
|
|
|
|
color = YELLOW if cpu <= 50 else ORANGE if cpu <= 75 else RED
|
|
|
|
|
cpu_str = f"{cpu:6.2f}%"
|
|
|
|
|
return f"{color}{cpu_str} {name[:25]:<25} {user[:15]:<15}{RESET_TO_GREEN}"
|
2025-07-31 14:04:19 -07:00
|
|
|
|
2025-07-31 14:37:29 -07:00
|
|
|
def print_processes(limit, show_quit_hint=True):
|
2025-07-31 14:08:27 -07:00
|
|
|
clear_screen()
|
2025-07-31 14:04:19 -07:00
|
|
|
top_processes = get_top_processes(limit)
|
|
|
|
|
|
2025-07-31 14:27:49 -07:00
|
|
|
print(GREEN, end='')
|
|
|
|
|
|
|
|
|
|
print(" CPU Process User")
|
2025-07-31 14:08:27 -07:00
|
|
|
print("----------------------------------------------")
|
2025-07-31 14:04:19 -07:00
|
|
|
|
2025-07-31 14:08:27 -07:00
|
|
|
for (cpu, name, user) in top_processes:
|
2025-07-31 14:04:19 -07:00
|
|
|
print(colorize(cpu, name, user))
|
|
|
|
|
|
2025-07-31 14:37:29 -07:00
|
|
|
if show_quit_hint:
|
|
|
|
|
print("\nPress 'q' to quit.")
|
2025-07-31 14:08:27 -07:00
|
|
|
|
|
|
|
|
def wait_for_keypress(timeout=1.0):
|
|
|
|
|
fd = sys.stdin.fileno()
|
|
|
|
|
old_settings = termios.tcgetattr(fd)
|
|
|
|
|
try:
|
|
|
|
|
tty.setcbreak(fd)
|
|
|
|
|
if select.select([sys.stdin], [], [], timeout)[0]:
|
|
|
|
|
return sys.stdin.read(1)
|
|
|
|
|
finally:
|
|
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
|
|
|
|
return None
|
|
|
|
|
|
2025-07-31 14:04:19 -07:00
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser(description="Monitor system processes by CPU usage.")
|
|
|
|
|
parser.add_argument("-n", "--number", type=int, default=10, help="Number of processes to monitor (default is 10).")
|
2025-07-31 14:16:01 -07:00
|
|
|
parser.add_argument("-i", "--interval", type=int, default=1, help="Refresh interval in seconds (default is 1).")
|
2025-07-31 14:04:19 -07:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
limit = args.number
|
2025-07-31 14:16:01 -07:00
|
|
|
interval = max(1, args.interval)
|
2025-07-31 14:08:27 -07:00
|
|
|
|
2025-07-31 14:37:29 -07:00
|
|
|
refresh_count = 0
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
2025-07-31 14:04:19 -07:00
|
|
|
try:
|
|
|
|
|
while True:
|
2025-07-31 14:37:29 -07:00
|
|
|
refresh_count += 1
|
|
|
|
|
elapsed = time.time() - start_time
|
|
|
|
|
show_quit = refresh_count <= 3 or elapsed <= 3
|
|
|
|
|
print_processes(limit, show_quit_hint=show_quit)
|
2025-07-31 14:16:01 -07:00
|
|
|
key = wait_for_keypress(timeout=interval)
|
2025-07-31 14:08:27 -07:00
|
|
|
if key and key.lower() == 'q':
|
|
|
|
|
print("Exiting...")
|
|
|
|
|
break
|
2025-07-31 14:04:19 -07:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print("\nProgram interrupted. Exiting...")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|
|
|
|
|
|