systemd module¶
SystemdDependencies
¶
A unit's dependency edges, as a dict of property -> list of unit names.
Commands:
systemctl show <unit> --property=Requires,Wants,After,Before,Conflicts
Returns:
dict[str, list[str]]
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_dependencies.py.
SystemdEnabled
¶
Whether each systemd unit is enabled on boot, as a dict of unit -> bool.
Commands:
systemctl show --all --property Id --property UnitFileState '*'
Returns:
dict[str, bool]
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_enabled.py.
SystemdFailedUnit
¶
Attributes:
| Name | Type | Description |
|---|---|---|
unit |
The failed unit's full name (e.g. |
|
load |
The unit's load state ( |
|
active |
The unit's active state ( |
|
sub |
The unit's sub-state ( |
|
description |
The unit's description, the table's trailing column. |
SystemdFailedUnits
¶
The failed systemd units, as a list of SystemdFailedUnit dicts.
Commands:
systemctl --failed --no-legend --no-pager
Returns:
list[SystemdFailedUnit]
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_failed_units.py.
SystemdServiceEnabled
¶
Whether a unit is enabled on boot.
Commands:
systemctl show -p UnitFileState --value <unit>
Returns:
bool
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/server/wait_for_httpd.py.
SystemdServiceState
¶
A unit's systemd ActiveState (active, inactive, ...), or None.
Commands:
systemctl show -p ActiveState --value <unit>
Returns:
str | None
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/server/wait_for_httpd.py.
SystemdStatus
¶
Whether each systemd unit is active, as a dict of unit name -> bool.
Commands:
systemctl show --all --property Id --property SubState '*'
Returns:
dict[str, bool]
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_status.py.
SystemdSystemState
¶
The system manager's overall health word (running, degraded, ...).
Commands:
systemctl is-system-running || true
Returns:
str
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_system_state.py.
SystemdTimer
¶
Attributes:
| Name | Type | Description |
|---|---|---|
active_state |
The timer's |
|
unit_file_state |
The timer's |
|
next_elapse_usec |
The next elapse time in microseconds since the
epoch, or None when systemd reports |
|
last_trigger_usec |
The last trigger time in microseconds since the
epoch, or None when the timer has never fired ( |
SystemdTimers
¶
The *.timer units and their schedule, as a dict of unit -> SystemdTimer.
Commands:
systemctl show '*.timer' --property=Id,ActiveState,UnitFileState,NextElapseUSecRealtime,LastTriggerUSec
Returns:
dict[str, SystemdTimer]
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_timers.py.
SystemdUnitFile
¶
Attributes:
| Name | Type | Description |
|---|---|---|
state |
The unit file's state ( |
|
preset |
The unit file's preset policy ( |
SystemdUnitFiles
¶
The installed unit files and their state/preset, as a dict of unit -> SystemdUnitFile.
Commands:
systemctl list-unit-files --no-legend --no-pager
Returns:
dict[str, SystemdUnitFile]
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_unit_files.py.
SystemdUnitProperties
¶
A unit's curated systemctl show properties, as raw strings.
Commands:
systemctl show <unit> --property=Id,Description,LoadState,ActiveState,SubState,UnitFileState,FragmentPath,MainPID
Returns:
dict[str, str]
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_unit_properties.py.
SystemdVersion
¶
The systemd release and compiled-in features, as a SystemdVersionInfo dict.
Commands:
systemctl --version
Returns:
SystemdVersionInfo
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_systemd_version.py.
SystemdVersionInfo
¶
Attributes:
| Name | Type | Description |
|---|---|---|
version |
The upstream systemd integer version (e.g. |
|
release |
The distribution release string in parentheses (e.g.
|
|
features |
The compiled-in feature flags line (e.g.
|
daemon_reload
async
¶
daemon_reload(
ctx: OpContext,
user_mode: bool = False,
machine: str | None = None,
user_name: str | None = None,
**kwargs: Any,
) -> AsyncGenerator[Command]
Reload the systemd daemon to read unit file changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
user_mode
|
bool
|
Use the per-user systemd manager
( |
False
|
machine
|
str | None
|
Connect to a named machine's systemd
( |
None
|
user_name
|
str | None
|
Connect to a specific user's systemd session. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
A single |
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_daemon_reload.py.
dropin
async
¶
dropin(
ctx: OpContext,
unit: str,
filename: str,
content: str,
present: bool = True,
daemon_reload: bool = True,
restart: bool = False,
**kwargs: Any,
) -> AsyncGenerator[Command]
Add/remove/update a systemd unit drop-in configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
unit
|
str
|
The unit the drop-in extends (a bare name gets
|
required |
filename
|
str
|
The drop-in name; the file is
|
required |
content
|
str
|
The drop-in's text content (the |
required |
present
|
bool
|
Whether the drop-in file should exist. Defaults to True. |
True
|
daemon_reload
|
bool
|
Reload the systemd daemon after a write/removal so the change takes effect. Defaults to True. |
True
|
restart
|
bool
|
Restart the unit after a write so the new drop-in is applied to the running process. Defaults to False. |
False
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
The spool transfer + elevated install (and daemon-reload /
restart) when the content differs, or a removal (+ daemon-reload)
when |
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_dropin.py.
mask
async
¶
mask(
ctx: OpContext,
unit: str,
masked: bool = True,
user_mode: bool = False,
machine: str | None = None,
user_name: str | None = None,
**kwargs: Any,
) -> AsyncGenerator[Command]
Mask or unmask a systemd unit so it cannot be started.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
unit
|
str
|
Name of the unit ( |
required |
masked
|
bool
|
Whether the unit should be masked
( |
True
|
user_mode
|
bool
|
Use the per-user systemd manager
( |
False
|
machine
|
str | None
|
Connect to a named machine's systemd
( |
None
|
user_name
|
str | None
|
Connect to a specific user's systemd session. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
A single |
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_mask.py.
preset
async
¶
preset(
ctx: OpContext,
unit: str | None = None,
all_units: bool = False,
user_mode: bool = False,
machine: str | None = None,
user_name: str | None = None,
**kwargs: Any,
) -> AsyncGenerator[Command]
Apply a unit's preset policy (enable/disable per the vendor preset).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
unit
|
str | None
|
The unit to preset (a bare name gets
|
None
|
all_units
|
bool
|
Apply presets to every unit
( |
False
|
user_mode
|
bool
|
Use the per-user systemd manager
( |
False
|
machine
|
str | None
|
Connect to a named machine's systemd
( |
None
|
user_name
|
str | None
|
Connect to a specific user's systemd session. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
A |
Raises:
CommandNotFoundError, HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/systemd/systemd_preset.py.
reset_failed
async
¶
reset_failed(
ctx: OpContext,
unit: str,
user_mode: bool = False,
machine: str | None = None,
user_name: str | None = None,
**kwargs: Any,
) -> AsyncGenerator[Command]
Clear a systemd unit's failed state with reset-failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
unit
|
str
|
Name of the unit (a bare name gets |
required |
user_mode
|
bool
|
Use the per-user systemd manager
( |
False
|
machine
|
str | None
|
Connect to a named machine's systemd
( |
None
|
user_name
|
str | None
|
Connect to a specific user's systemd session. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
A single |
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in
examples/modules/systemd/systemd_reset_failed.py.
service
async
¶
service(
ctx: OpContext,
service: str,
running: bool = True,
restarted: bool = False,
reloaded: bool = False,
enabled: bool | None = None,
daemon_reload: bool = False,
reset_failed: bool = False,
soft_reload: bool = False,
now: bool = False,
user_mode: bool = False,
machine: str | None = None,
user_name: str | None = None,
**kwargs: Any,
) -> AsyncGenerator[Command]
Manage the state of a systemd unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
service
|
str
|
Name of the unit ( |
required |
running
|
bool
|
Whether the unit should be running. Defaults to True. |
True
|
restarted
|
bool
|
Whether to restart the unit (only if it is already running). Defaults to False. |
False
|
reloaded
|
bool
|
Whether to reload the unit (only if it is already running). Defaults to False. |
False
|
enabled
|
bool | None
|
Whether the unit should be enabled
on boot; |
None
|
daemon_reload
|
bool
|
Reload the systemd daemon to read updated unit files before anything else. Defaults to False. |
False
|
reset_failed
|
bool
|
Clear a |
False
|
soft_reload
|
bool
|
When |
False
|
now
|
bool
|
When |
False
|
user_mode
|
bool
|
Use the per-user systemd manager
( |
False
|
machine
|
str | None
|
Connect to a named machine's systemd
( |
None
|
user_name
|
str | None
|
Connect to a specific user's
systemd session ( |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated systemctl commands. |
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_service.py.
sysusers
async
¶
sysusers(
ctx: OpContext,
filename: str,
content: str,
present: bool = True,
**kwargs: Any,
) -> AsyncGenerator[Command]
Add/remove/update a sysusers.d configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
filename
|
str
|
The config name; the file is |
required |
content
|
str
|
The sysusers.d text content (the type/name/id/... rows). |
required |
present
|
bool
|
Whether the config file should exist. Defaults to True. |
True
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
The spool transfer + elevated install + |
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_sysusers.py.
tmpfiles
async
¶
tmpfiles(
ctx: OpContext,
filename: str,
content: str,
present: bool = True,
**kwargs: Any,
) -> AsyncGenerator[Command]
Add/remove/update a tmpfiles.d configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
filename
|
str
|
The config name; the file is |
required |
content
|
str
|
The tmpfiles.d text content (the type/path/mode/... rows). |
required |
present
|
bool
|
Whether the config file should exist. Defaults to True. |
True
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
The spool transfer + elevated install + |
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in examples/modules/systemd/systemd_tmpfiles.py.
unit_name
¶
unit_name(service: str) -> str
Normalize a service name to a full systemd unit name.
httpd becomes httpd.service; a name already carrying a systemd
unit suffix (logrotate.timer, httpd.service) is returned as is.
Both the facts and the operations key on the normalized name, so the
test framework's simulated state must use full unit names too.