osexp2000
osexp2000

Reputation: 3165

systemd: an service unit WITHOUT '[Install]' section, will it be auto run on boot?

for such a simple question, I just can not find a straight forward document.

If I create a service unit, place it in /etc/systemd/system/a.service,

[Unit]
Requires=network-online.target
After=network-online.target
[Service]
ExecStart=/bin/bash /etc/a.sh

See I omitted the [Install] section and normal systemctl enable ... step

[Install]
WantedBy=multi-user.target

Then when the machine reboot, the service will not get run. I know this make sense, it seems systemd is trying to find all /etc/systemd/system/.wants/.service to run,

Is this the only way to get a new service auto started?

The reason why I asked this is that want to statically simply analyze services inside a system image without running it so need to figure out which services will be auto started.

Upvotes: 5

Views: 3869

Answers (2)

osexp2000
osexp2000

Reputation: 3165

I found that a service can be defined without the [Install] section, this kind of service will be showed as "static" in systemctl status SERVICE

See http://manpages.ubuntu.com/manpages/bionic/man1/systemctl.1.html

           │"static"          │ The unit file is not     │ 0         │
           │                  │ enabled, and has no      │           │
           │                  │ provisions for enabling  │           │
           │                  │ in the "[Install]" unit  │           │
           │                  │ file section.            │          

The systemctl enable SERVICE has no effect because systemctl does not know where it should create a symlink of the service.

Users must manually create the symlink. Such as systemd-modules-load.service is a static service:

Its status is a little bit frightening because it is not showed as "enabled".

# systemctl status systemd-modules-load
● systemd-modules-load.service - Load Kernel Modules
   Loaded: loaded (/usr/lib/systemd/system/systemd-modules-load.service; static; vendor preset: disabled)
   Active: inactive (dead)
Condition: start condition failed at Mon 2021-11-29 13:28:38 UTC; 4 days ago
           none of the trigger conditions were met
     Docs: man:systemd-modules-load.service(8)
           man:modules-load.d(5)

let us look at its definition

# systemctl cat systemd-modules-load
# /usr/lib/systemd/system/systemd-modules-load.service
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Load Kernel Modules
Documentation=man:systemd-modules-load.service(8) man:modules-load.d(5)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-readahead-collect.service systemd-readahead-replay.service
Before=sysinit.target shutdown.target
ConditionCapability=CAP_SYS_MODULE
ConditionDirectoryNotEmpty=|/lib/modules-load.d
ConditionDirectoryNotEmpty=|/usr/lib/modules-load.d
ConditionDirectoryNotEmpty=|/usr/local/lib/modules-load.d
ConditionDirectoryNotEmpty=|/etc/modules-load.d
ConditionDirectoryNotEmpty=|/run/modules-load.d
ConditionKernelCommandLine=|modules-load
ConditionKernelCommandLine=|rd.modules-load

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/lib/systemd/systemd-modules-load

And how does it get statically enabled:

# find /etc /lib/ -name 'systemd-modules-load.service' 2>/dev/null |xargs ls -lFhA
lrwxrwxrwx. 1 root root   31 Oct 30  2020 /lib/systemd/system/sysinit.target.wants/systemd-modules-load.service -> ../systemd-modules-load.service
-rw-r--r--. 1 root root 1.1K Oct  1  2020 /lib/systemd/system/systemd-modules-load.service

The /lib/systemd/system/sysinit.target.wants/systemd-modules-load.service make it enabled.

Upvotes: 1

Lucas Werkmeister
Lucas Werkmeister

Reputation: 2742

The [Install] section just instructs systemd how the service should be enabled. What actually matters for getting a service started is that it is pulled in by a target, usually via a symlink in the target’s .wants/ directory. That symlink can be created by systemctl enable using the information in the [Install] section, but can just as well be created manually.

Upvotes: 4

Related Questions