0

Using systemd, how can I configure a service not to start at boot, even if it was running when I most recently shut down? I know that I can disable a service, but if I start that service, then reboot, it will continue to run. I want this:

  1. Start the service now.
  2. Reboot.
  3. Service is not active/running.

I searched the web for an answer, but I found too much noise from people asking and answering about starting/stopping and enabling/disabling services in general. From what I can tell, I suppose I want to explicitly disable the service at boot time. I'm an Advanced Beginner with Linux. Where would I put this instruction to ensure that it happens at the appropriate point in the boot sequence?

Or is there another sensible and reliable way to do this?

UPDATE: I decided, in the end, that I shouldn't install this as a service at all. This seems obvious, looking back.

5
  • How do you start the service? Starting a service shouldn’t enable it... – Stephen Kitt Dec 14 '19 at 8:47
  • @StephenKitt I started it with systemctl start. I was surprised, too, when it ran on reboot. systemctl list-unit-files lists the service as disabled. – J. B. Rainsberger Dec 14 '19 at 8:48
  • It would be nice to motivate your question. What is the actual case where you want to do this? – Basile Starynkevitch Dec 14 '19 at 9:05
  • Could you give the linux distribution and the service name showing this behaviour? Also some services have "socket activation", perhaps that's what happened (eg on Debian, there's ssh.service and also ssh.socket, you can disable the former and enable the later) – A.B Dec 14 '19 at 13:13
  • anyway, that's probably this, but since I'm not well versed in systemd, I'd rather not do an answer: systemd: Masking units (If it works, post it yourself as answer please). – A.B Dec 14 '19 at 13:28
1

The foo service starting program (which you need to improve) could be something testing the presence of a given file, e.g. /var/tmp/dont_start_foo_service, and if that file is present don't do anything more so exit immediately.

In your C code, use access(2) to test the existence of a file.

In your shell scripts, use test(1) for that, e.g. [ -f /var/tmp/dont_start_foo_service ];

See also hier(7) for file name conventions, and /etc/nologin documented in nologin(5) as prior art.

To create (before previous shutdown or reboot) that /var/tmp/dont_start_foo_service you could use touch(1) (or creat(2) in C code).

You would forcibly and explicitly remove (e.g. with rm(1) or remove(3) ...) that /var/tmp/dont_start_foo_service before systemctl start to really start foo.

You could even want to use some sqlite database or JSON file instead of that /var/tmp/dont_start_foo_service which would somehow store your dontstart flag.

Consider also crontab(5) jobs with @reboot time specifications, or at or batch jobs (see this).

7
  • None of this seems to relate to systemd, so I'm confused. I want the equivalent of "stop this service whenever I boot". – J. B. Rainsberger Dec 14 '19 at 8:45
  • That "stop this service whenever I boot" is exactly touch /var/tmp/dont_start_foo_service and of course you need to configure your foo service per the hints given above – Basile Starynkevitch Dec 14 '19 at 8:49
  • I want to be able to start the service using systemctl start. If I change the start script/command, that would prevent me from starting the service at all using systemctl start, would it not? – J. B. Rainsberger Dec 14 '19 at 8:53
  • No, your starting script will test existence of /var/tmp/dont_start_foo_serviceand exit prematurely if that file exists – Basile Starynkevitch Dec 14 '19 at 8:54
  • So I am supposed to remember to create that file before I shut down my machine, but remove that file when I want to start the service? My goal is to always stop (not merely never start) the service at boot, but otherwise be able to start and stop the service on demand. – J. B. Rainsberger Dec 14 '19 at 8:56

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.