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.

  • 1
    I 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 Jul 6 '15 at 16:33
  • @MarkLakata thanks for the heads up, you potentially saved me a lot of trouble – haventchecked Jan 27 '16 at 18:13

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 \

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

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.