17

I am reading https://www.freedesktop.org/software/systemd/man/systemd.service.html and can not find information if [Install] section is required for service autostart.

1 Answer 1

17

The standard way to make some program executed on startup with systemd is to create .service file for it, place that file into corresponding directory and run systemctl enable <service> to enable it for startup sequence. [Install] section is mandatory here, because it tells systemd at which moment during boot process your service should be started. You process should be linked to some generic boot targets such as multi-user.target or graphical.target, or to a special purpose target (such as network-online.target), or a custom local target.

Example:

[Install]
WantedBy=multi-user.target

Here systemd will inject your service as a dependency for multi-user.target. systemd will start your service whenever multi-user target is started.

systemd reads files (or symlinks) in its configuration directories to see which units should be started in what order. systemctl enable creates such symlinks for services that it already knows, and places these symlinks at the points at the boot process when the service should be started (e.g. in special multi-user.target.wants/ subdirectory.)


There is also another way how operating system uses systemd to start its own services at startup. It is not something that should you do, but since the question is about [Install] section...

There are systemd units called "static" units and they are not managed by systemctl enable (or systemctl disable.) They are started on boot through hardcoded symlinks in /usr/lib/systemd/system/ (instead of /etc/systemd/system/), and if you encounter them while looking at units in your system... know that they don't have [Install] section.

(You can see the discussion in this forum post for even more details on static units.)

8
  • OMG. That's surely confusing! I thought there is only one way to autostart systemd services. That is - with systemctl enable. Now I need to study a whole chapter about those "static". =) Commented Nov 27, 2018 at 7:32
  • Actually, for the most part you should just ignore "static" units. They're meant to be used by the Linux distribution, for services they want to always start and not allow the administrator to control them... In practice, you'll always want to use non-static units, so always include an [Install] section is a good advice.
    – filbranden
    Commented Nov 27, 2018 at 7:37
  • 1
    Well, the [Install] section is obligatory for .services that you want to manage with systemctl enable. I'd argue that in 99% of cases, you want to be able to manage your .services with systemctl enable. If you're curious, there's also this other thing called "static" units, which will also auto-start on boot, but they don't have an [Install] section (and can't be managed with systemctl disable), but that feature is meant to be used by Linux distributions only, so you should really not care about it, and always include an [Install] section. :-)
    – filbranden
    Commented Nov 27, 2018 at 7:55
  • 2
    I edited you answer with all info I wanted to know, but was afraid to ask. Please, check that I did't misguide anyone. I thought that symlinking is too low level detail until I understand high level logic, so I omitted it until the very end. Commented Nov 27, 2018 at 12:16
  • 1
    @anatolytechtonik I reviewed your edits, moved things around a little bit and approved it. One point is that "static" units are not as opposed to "service" units. You can have units of any kind ("service", "target", "socket", "path") that are "static", so it's an orthogonal concept. Thanks for your edit, your suggestions were great. Cheers!
    – filbranden
    Commented Nov 27, 2018 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .