Skip to content

systemd_util

Utilities for interacting with systemd units.

Functions:

  • does_unit_exist

    Check if a systemd unit is defined and loaded. This does not check if the unit is running.

  • get_command_line

    Read the command line arguments for a process from /proc/[pid]/cmdline.

  • get_unit_pid

    For a running systemd unit, return the PID of the main process. Otherwise, return None.

  • get_unit_property

    Return the value of a property of a systemd unit.

does_unit_exist

does_unit_exist(unit_name: str) -> bool

Check if a systemd unit is defined and loaded. This does not check if the unit is running.

Parameters:

  • unit_name (str) –

    The name of the systemd unit to check.

Returns:

  • bool

    True if the unit is defined and loaded, False otherwise.

get_command_line

get_command_line(pid: int) -> list[str]

Read the command line arguments for a process from /proc/[pid]/cmdline.

Parameters:

  • pid (int) –

    The process ID.

Returns:

  • list[str]

    List of command line arguments.

get_unit_pid

get_unit_pid(unit_name: str) -> int | None

For a running systemd unit, return the PID of the main process. Otherwise, return None.

Parameters:

  • unit_name (str) –

    The name of the systemd unit to get the PID of.

Returns:

  • int | None

    The PID of the main process of the unit, or None if the unit is not running.

get_unit_property

get_unit_property(
    unit_name: str, property_name: str
) -> str

Return the value of a property of a systemd unit.

Parameters:

  • unit_name (str) –

    The name of the systemd unit to get the property of.

  • property_name (str) –

    The name of the property to get the value of.

Raises:

  • KeyError

    If the property is not found for the unit.

Returns:

  • str

    The value of the property.