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.
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
To answer your second question, os.system('some systemctl command') is just as valid a strategy as using the library. The library centralizes error handling strategies and best practices for doing things like stopping and masking a service, which might save you some boilerplate and some trips to the systemd docs. It’s there to use if you’d like, and there to ignore if you don’t
Hrm. I may be mistaken. I thought that we had published a charm to hang the libs off of, but you are correct that its source cannot be found in the operator-libs-linux repo …
There was a test charm for a while, but then we moved to a more charm-independent test infra using LXD in this case. I’ll add a TODO to my list for next week to get the hello-juju charm updated to use the apt and systemd libraries, though