systemd

Abstractions for stopping, starting and managing system services via systemd.

For the most part, we transparently provide an interface to a commonly used selection of systemd commands, with a few shortcuts baked in. For example, service_pause and service_resume will run the mask/unmask and enable/disable invocations.

Example usage

from charmlibs import systemd

# Start a service
if not systemd.service_running("mysql"):
    success = systemd.service_start("mysql")

# Attempt to reload a service, restarting if necessary
success = systemd.service_reload("nginx", restart_on_failure=True)
exception SystemdError

Bases: Exception

Custom exception for systemd related errors.

daemon_reload() bool

Reload systemd manager configuration.

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if systemctl daemon-reload returns a non-zero returncode.

service_disable(*args: str) bool

Disable a system service.

Parameters:

*args – Arguments to pass to systemctl disable (normally the service name).

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if systemctl disable … returns a non-zero returncode.

service_enable(*args: str) bool

Enable a system service.

Parameters:

*args – Arguments to pass to systemctl enable (normally the service name).

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if systemctl enable … returns a non-zero returncode.

service_failed(service_name: str) bool

Report whether a system service has failed.

Parameters:

service_name – The name of the service to check.

Returns:

True if service is marked as failed; False if not.

service_pause(service_name: str) bool

Pause a system service.

Stops the service and prevents the service from starting again at boot.

Parameters:

service_name – The name of the service to pause.

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if service is still running after being paused by systemctl.

service_reload(service_name: str, restart_on_failure: bool = False) bool

Reload a system service, optionally falling back to restart if reload fails.

Parameters:
  • service_name – The name of the service to reload.

  • restart_on_failure – Boolean indicating whether to fall back to a restart if the reload fails.

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if systemctl reload|restart … returns a non-zero returncode.

service_restart(*args: str) bool

Restart a system service.

Parameters:

*args – Arguments to pass to systemctl restart (normally the service name).

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if systemctl restart … returns a non-zero returncode.

service_resume(service_name: str) bool

Resume a system service.

Re-enable starting the service again at boot. Start the service.

Parameters:

service_name – The name of the service to resume.

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if service is not running after being resumed by systemctl.

service_running(service_name: str) bool

Report whether a system service is running.

Parameters:

service_name – The name of the service to check.

Returns:

True if service is running/active; False if not.

service_start(*args: str) bool

Start a system service.

Parameters:

*args – Arguments to pass to systemctl start (normally the service name).

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if systemctl start … returns a non-zero returncode.

service_stop(*args: str) bool

Stop a system service.

Parameters:

*args – Arguments to pass to systemctl stop (normally the service name).

Returns:

On success, this function returns True for historical reasons.

Raises:

SystemdError – Raised if systemctl stop … returns a non-zero returncode.