9

I am using systemd to handle some tasks and I have a service file which works fine once enabled with systemctl.

Now, I would like to enable it automatically from the first boot.

I know that putting a replacement file into /etc/systemd/system/ replaces the behavior of the file with the same name into /lib/systemd/system/.

There is a way to enable a service file automatically just to putting it in some directory ?

3 Answers 3

15

IMPORTANT NOTE: The following works for me under Ubuntu. It should work as is under Debian. RPM based distros prevent the auto-start by default, but it may still get you closer to your goal.

In most cases, you want to install it in the multi-user.target using the install section as follow:

[Install]
WantedBy=multi-user.target

This means the your-package.postinst script will automatically start the daemon for you.

Note that if you have your own your-package.postinst script, you have to make sure to include the Debian helper as in:

#!/bin/sh

#DEBHELPER#

...your own script here...

Without the #DEBHELPER# pattern, the packager will not add the default code and as a result your daemon won't get enabled and started automatically. The code added there will enable and start the service:

systemctl enable <service-name>
systemctl start <service-name>

unless the service is static (does not have an [Install] section as shown above.) A static service requires a special command line to be enabled and that's not available by default:

systemctl add-wants multi-user.target <service-name>

As we can see, the command includes multi-user.target which the default #DEBHELPER# (systemd, really) has no clue about unless you have an [Install] section.


Update: In newer versions, the systemd extension is now part of the installation mechanism and the following is not required anymore. For Ubuntu, I think that started with 22.04, maybe 21.10.

The package must also be built with the systemd extension. This means having the following in your debian/rules file:

%:
    dh $@ --with systemd --parallel

That should get you set.


Just in case, if you wanted to not start the newly installed service, you can actually prevent such by adding the following:

override_dh_installsystemd:
    dh_installsystemd --no-start --no-disable

Mix and match as required by your system.

5

sudo systemctl enable <service-name> will enable service at boot time.

http://www.dynacont.net/documentation/linux/Useful_SystemD_commands/ has a list of useful commands. And there's always man systemctl and man systemd.

1
  • 2
    Note that you need at least the statement WantedBy=multi-user.target under [Install] in the unit file for it to start on boot in addition to enabling it via systemctl, otherwise the enable command won't do anything.
    – Mahn
    Commented Jul 13, 2016 at 0:45
1

You could use systems.preset for default enabled units.

Systems.preset allows to configure set of units to be enabled/disabled

vendor preset in systemctl status unit refers to this.

For more info how use this refer linked man page

You must log in to answer this question.

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