Newly published systemd library for Machine Operators

Hi All,

I just published a systemd library. It is intended for use in machine charms that use the Operator Framework.

To use it, run the following from within the directory of a charm in which you wish to use the lib:

charmcraft fetch-lib charms.operator_libs_linux.v0.systemd

It then works like so:

from systemd import service_running, service_start, service_reload

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

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

The library current supports the following interactions:

On the fly control of a service via service_start, service_stop, service_restart and service_reload.

Pause (stop and mask) a service until you explicitly resume it via service_pause and service_resume.

This is largely based on the legacy code in charmhelpers, so most of the library should be familiar to you if you have used that library in the past. Specifically, the new library retains the prior behavior of returning a boolean value for success or failure, rather than raising an exception. This makes it easy to run checks such as is_running or service_restart with minimal exception handling.

To file bugs, request enhancements, and submit patches (patches are always welcome!), go here:, or ping me (@pengale) in this thread.

~ Penny



Can you perhaps create a charm in this repo to help people get started with this awesome library ?

I can help you test and make it tidy. We can also showcase it in some community workshop in a week or two?

1 Like

Check this out @hallback @tmihoc @jamesbeedy @heitor

1 Like

Hey Erik!

Glad you like the library. We’ll have a bunch of charms that will use this soon, and I’m hoping that the integration tests and docstring up the top should be a good example, too :slight_smile:

1 Like

@jnsgruk sounds cool.

A friend of mine asked a valid question why one should use this lib, adding in more external sources as opposed to just

os.system('systemctl xxxxxx myservice')

What is the gain and loss of using the lib?