-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
88 lines (73 loc) · 3.12 KB
/
main.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import platform
# Check if the script is running on Windows
if platform.system() != "Windows":
print("This script is intended for use on Windows systems only.")
exit()
import psutil
from tabulate import tabulate
from datetime import datetime
def list_processes(sort_by=None, filter_status=None):
# Get the list of all processes
processes = psutil.process_iter(['pid', 'name', 'status', 'memory_info', 'cpu_times', 'username', 'create_time'])
process_list = []
for proc in processes:
try:
# Retrieve process information
pid = proc.info['pid']
name = proc.info['name']
status = proc.info['status']
memory_info = proc.info['memory_info']
cpu_times = proc.info['cpu_times']
username = proc.info['username']
create_time = datetime.fromtimestamp(proc.info['create_time']).strftime("%Y-%m-%d %H:%M:%S")
memory = memory_info.rss / (1024 * 1024) # Convert bytes to MB
cpu_time = cpu_times.user + cpu_times.system
# Check if a specific status is filtered or show all
if not filter_status or status.lower() == filter_status.lower():
process_list.append([pid, name, status, f"{memory:.2f} MB", f"{cpu_time:.2f} sec", username, create_time])
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
continue
# Sort processes by the specified field
if sort_by:
sort_fields = {
'pid': 0,
'name': 1,
'status': 2,
'memory': 3,
'cpu_time': 4,
'username': 5,
'create_time': 6
}
process_list.sort(key=lambda x: x[sort_fields[sort_by]])
# Print the process information in a tabular format
headers = ["PID", "Name", "Status", "Memory Usage", "CPU Time", "Username", "Creation Time"]
print(tabulate(process_list, headers=headers, tablefmt="grid"))
def get_user_input():
print("Process List Options:")
print("1. No Sorting")
print("2. Sort by PID")
print("3. Sort by Name")
print("4. Sort by Status")
print("5. Sort by Memory Usage")
print("6. Sort by CPU Time")
print("7. Sort by Username")
print("8. Sort by Creation Time")
sort_options = {
'1': None,
'2': 'pid',
'3': 'name',
'4': 'status',
'5': 'memory',
'6': 'cpu_time',
'7': 'username',
'8': 'create_time'
}
sort_choice = input("Enter the number corresponding to your sort choice: ")
sort_by = sort_options.get(sort_choice, None)
filter_status = input("Enter the process status to filter by (e.g., running, sleeping) or leave blank for no filter: ")
return sort_by, filter_status
if __name__ == "__main__":
sort_by, filter_status = get_user_input()
list_processes(sort_by, filter_status)
# Add a pause at the end of the script to prevent the window from closing immediately
input("Press Enter to exit...")