2

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 ?

4

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 case, 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.

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.

1

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
    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 Jul 13 '16 at 0:45
0

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

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.