Preferred/Idiomatic method to enable a systemd.service from a Debian package installation

5

1

I have a custom Debian package I've built for our software that creates a systemd service. What is the preferred/idiomatic way to have the installation of said package enable/start the service at install time?

What I did was add the following three lines to my postinst script:

systemctl --system daemon-reload
systemctl enable my_service.service
systemctl start my_service.service

This worked fine until we tried to include loading this package in our initial image creation running under debootstrap at which point it turned out that systemctl appears to want a lot of environment stuff set up that just isn't normally up in that process.

Travis Griggs

Posted 2014-11-05T19:09:18.687

Reputation: 641

1I have a similar question related to RPMs. I eventually gave up trying to start the system from the RPM post install script, because the way yum upgrade works: it first installs the new package, then uninstalls the old package. That means that the service is started, then stopped if you do an upgrade! – Mark Lakata – 2015-07-06T16:33:41.010

@MarkLakata thanks for the heads up, you potentially saved me a lot of trouble – haventchecked – 2016-01-27T18:13:22.920

Answers

1

I'm looking at a similar situation, I want to configure services in a debootstrap rootfs. It seems systemctl enable simply reads the targets in the [Install] section of a unit file and creates the correct symlinks. So if you know what services you want to start under which targets, I think the correct answer might be to just create the correct symlinks. e.g.

ln -s /etc/systemd/system/multi-user.target.wants/ssh.service \
      /lib/systemd/system/ssh.service

The other two steps (daemon-reload and start) you don't want to do when installing into a debootstrap chroot, but then I guess your problem is how to just do the symlink when in the debootstrap env and the normal way otherwise....

Edit for debootstrap

As it happens I'm also using debootstrap and I've found systemctl enable calls work fine so long as the calling user is root (to avoid the Failed to connect to bus warning.) daemon-reload and start probably don't make sense during an install under chroot since you're not in a live environment. So your postinst probably wants to detect chroot and skip those lines. Or set an env variable or pass some flag that your installer script can read.

IM_DEBOOTSTRAPPING=yes dpkg -i mypackage.deb  # tells postinst to skip systemctl start

Thom Nichols

Posted 2014-11-05T19:09:18.687

Reputation: 133