34

There is a service I want to run only when another service fails ([Unit] OnFailure=foo), but I don't want this service (foo) to start up automatically on boot. One option is running systemctl disable foo, but I'm looking for another way.

Background: I am creating an OS image, and I don't want to have to boot the machine up, run that command (systemctl disable foo), then shut it down before declaring my image final.

0

4 Answers 4

51

systemctl enable works by manipulating symlinks in /etc/systemd/system/ (for system daemons). When you enable a service, it looks at the WantedBy lines in the [Install] section, and plops symlinks in those .wants directories.

systemctl disable does the opposite.

You can just remove those symlinks—doing that by hand is fully equivalent to using systemctl disable.

4
  • This would be a service that hasn't been run by systemd yet (it's custom), so there wouldn't be any of those extra symlinks. I am guessing those would be created on first boot? Commented Apr 21, 2015 at 16:43
  • 3
    @Tshepang it shouldn't be enabled unless you enable it manually (or in a script, of course) Commented Apr 21, 2015 at 16:44
  • 1
    Oh, I assumed it would be enabled automatically (which is something I've come to expect by installing Debian packages). Good to know. Commented Apr 21, 2015 at 16:45
  • 4
    @Tshepang that's the postinst script in the package doing an enable. Or I guess shipping the symlinks in the package, not sure if they do that. Commented Apr 21, 2015 at 16:47
8

All services are "disabled" by default; "enablement" is opt-in.

If foo.service is your custom service, then it won't be started unless explicitly pulled in by something (like the OnFailure= directive), and you should not "disable" anything.

1
  • So basically you "get this functionality for free" unless foo has been enabled previously? Commented May 9, 2019 at 17:59
6

You can use systemctl start ServiceName.service and systemctl stop Service.Name.service to start and stop a service respectively. In contrast to systemctl enable ... and systemctl disable ..., the start and stop commands only last for the current session, therefore when you reboot the machine the changes wont be saved.

2

There's also the options of masking and unmasking, but someone with more experience and know-how should explain that instead of me.

sudo systemctl mask service_to_mask
sudo systemctl unmask service_to_unmask

I hope this is correct. But this type of thing needs to be in this conversation for completeness.

Masking uses diversion to /dev/null which is pretty nice.

If I remember correctly, services can be stopped, disabled, and masked in the same session.

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Feb 24, 2023 at 8:13
  • Unclear, but nevertheless quite correct — masking is also an alternative, especially when you have a complex configuration and do not want to dump it by symlinking to /dev/null. Masking allows systemd to sort of 'skip' that specific service as if it weren't there, but without deleting anything (systemctl will know it's there, and report it's masked, etc.). There might be some other reasons for masking vs. disabling services, which I'm also not the best person to explain them... that's the limit of my knowledge, I'm afraid. Commented Jun 24, 2024 at 18:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.