I am working on a configuration-as-code tool, and want to create systemd provider for it.

Now, suppose user describes in their configuration that they want NetworkManager.service enabled, and they want cups.service disabled.

We can (un)link corresponding unit files using systemctl enable NetworkManager.service and systemctl disable cups.service respectively, but now we need to stop services that need to be stopped and start services, that need to be started.

I was hoping that there is a command, that would do exactly that for me - "Stop everything that runs, but need to be stopped, and start everything that is stopped, but needs to run". systemctl daemon-reload is, unfortunately, not what I look for.

Is there such functionality built into systemctl, or will I have to manipulate every service individually?

P.S. This should actually be a separate question, but I suppose that there will be usecases, where my users will run my program from chrooted/containerized environment, where PID 1 is not systemd. Trying to invoke any unit manipulation actions leads to systemctl exiting with status 1 and message System has not been booted with systemd as init system (PID 1). Can't operate.. Is there a way I could check for such circumstances beforehand, or the best I can do is just trying and catching error?


To answer the original question:

daemon-reload just tells systemd to re-read its configuration.

Note that enabling a service means that the service starts as a dependency of other units. Assuming your default target is multi-user, you could try systemctl isolate multi-user.target; in my understanding, it should start all enabled units that depend on multi-user and stop all others.

If this doesn't work, a simple shell script that uses the output of systemctl list-units -t service should do the trick. EDIT: list-units doesn't report enabled/disabled; you could use systemctl status -t service \* instead.

Finally, perhaps you actually want this:

systemctl enable --now SERVICE
systemctl disable --now SERVICE

--now starts/stops the service at the same time as en/disabling it. Of course, you can also systemctl start SERVICE and systemctl stop SERVICE separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.