Operator Libs Linux

  • By Jon Seager
Channel Revision Published Runs on
latest/stable 2 09 Mar 2023
Ubuntu 22.04 Ubuntu 20.04
latest/stable 1 28 Oct 2021
Ubuntu 22.04 Ubuntu 20.04
juju deploy operator-libs-linux
Show information

Platform:

Ubuntu
22.04 20.04

charms.operator_libs_linux.v1.systemd

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

This library assumes that your charm is running on a platform that uses systemd. E.g., Centos 7 or later, Ubuntu Xenial (16.04) or later.

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 with run the mask/unmask and enable/disable invocations.

Example usage:

from charms.operator_libs_linux.v0.systemd import service_running, service_reload

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

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

class SystemdError

Description

Custom exception for SystemD related errors. None

def service_running(service_name: str)

Report whether a system service is running.

Arguments

service_name

The name of the service to check.

def service_failed(service_name: str)

Report whether a system service has failed.

Arguments

service_name

The name of the service to check.

Returns

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

def service_start()

Start a system service.

Arguments

*args

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

Returns

On success, this function returns True for historical reasons.

def service_stop()

Stop a system service.

Arguments

*args

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

Returns

On success, this function returns True for historical reasons.

def service_restart()

Restart a system service.

Arguments

*args

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

Returns

On success, this function returns True for historical reasons.

def service_enable()

Enable a system service.

Arguments

*args

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

Returns

On success, this function returns True for historical reasons.

def service_disable()

Disable a system service.

Arguments

*args

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

Returns

On success, this function returns True for historical reasons.

def service_reload(
    service_name: str,
    restart_on_failure: bool
)

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

Arguments

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.

def service_pause(service_name: str)

Pause a system service.

Arguments

service_name

The name of the service to pause.

Returns

On success, this function returns True for historical reasons.

Description

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

def service_resume(service_name: str)

Resume a system service.

Arguments

service_name

The name of the service to resume.

Returns

On success, this function returns True for historical reasons.

Description

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

def daemon_reload()

Reload systemd manager configuration.

Returns

On success, this function returns True for historical reasons.