Repository navigation
Expand file tree
/
Copy pathos_runtime
More file actions
57 lines (50 loc) · 1.79 KB
/
Copy pathos_runtime
File metadata and controls
57 lines (50 loc) · 1.79 KB
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
import platform
import subprocess
import time
def get_uptime_linux():
# On Linux, reading from /proc/uptime which gives uptime in seconds
try:
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
return uptime_seconds
except Exception as e:
print(f"Error getting uptime on Linux: {e}")
return None
def get_uptime_windows():
# On Windows, using 'systeminfo' to get the system boot time
try:
output = subprocess.check_output('net stats workstation', shell=True).decode()
for line in output.splitlines():
if "Statistics since" in line:
boot_time = line.split("since")[1].strip()
boot_time_struct = time.strptime(boot_time, "%m/%d/%Y %I:%M:%S %p")
boot_time_epoch = time.mktime(boot_time_struct)
uptime_seconds = time.time() - boot_time_epoch
return uptime_seconds
except Exception as e:
print(f"Error getting uptime on Windows: {e}")
return None
def format_runtime(seconds):
days = seconds // (24 * 3600)
seconds %= (24 * 3600)
hours = seconds // 3600
seconds %= 3600
minutes = seconds // 60
return f"{int(days)} days, {int(hours)} hours, {int(minutes)} minutes"
def get_system_uptime():
os_type = platform.system()
uptime_seconds = None
if os_type == "Linux":
uptime_seconds = get_uptime_linux()
elif os_type == "Windows":
uptime_seconds = get_uptime_windows()
else:
print("Unsupported OS")
return None
if uptime_seconds is not None:
return format_runtime(uptime_seconds)
return None
if __name__ == "__main__":
uptime = get_system_uptime()
if uptime:
print(f"System has been running for: {uptime}")