Uptime Library

3.0.1 · active · verified Thu Apr 16

The `uptime` library provides a cross-platform way to retrieve system uptime and boot time, aiming to do so without relying on external processes like parsing `uptime(1)` command output. It works on various major platforms, including Linux, Windows, macOS, and BSD systems. Despite its current version 3.0.1 being released in 2013, it remains a stable and widely used utility for basic system information.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch the system's uptime in seconds and its boot time as a `datetime` object. It includes error handling for cases where uptime or boot time cannot be determined.

from uptime import uptime, boottime
import datetime

def run_quickstart():
    try:
        system_uptime_seconds = uptime()
        if system_uptime_seconds is not None:
            print(f"System Uptime: {system_uptime_seconds:.2f} seconds")
            uptime_timedelta = datetime.timedelta(seconds=system_uptime_seconds)
            print(f"System Uptime (formatted): {uptime_timedelta}")
        else:
            print("Could not determine system uptime.")

        system_boot_time = boottime()
        if system_boot_time is not None:
            print(f"System Boot Time: {system_boot_time}")
        else:
            print("Could not determine system boot time.")

    except RuntimeError as e:
        print(f"An error occurred: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == '__main__':
    run_quickstart()

view raw JSON →