Systemd – Postinst Maintainer Script Stops Unit File from Starting Automatically

debiansystemdUbuntu

I'm creating a small Debian package to install systemd unit files, specifically *.service files.

My package looks like:

myservice
      | 
      |--debian
            |- comapat
            |- preinst
            |- postinst
            |- rules
            |- myservice.service

When I install my package, the systemd services are automatically disabled and they do not start up.

Right after install, systemctl status myservice shows

root@ubuntu-xenial:~# systemctl status myservice
  ● myservice.service - My Service - echos output and says when it started
  Loaded: loaded (/lib/systemd/system/myservice.service; disabled; vendor preset: enabled)
  Active: inactive (dead)

However, when I install my package without a postinst script, the systemd services are automatically enabled and they do start up after install.

root@ubuntu-xenial:~# systemctl status myservice
● myservice.service - MyService - echos output and says when it started
Loaded: loaded (/lib/systemd/system/myservice.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2018-02-09 15:06:36 UTC; 10s ago
Main PID: 5024 (sh)
Tasks: 2
Memory: 172.0K
  CPU: 1ms
CGroup: /system.slice/myservice.service
       ├─5024 /bin/sh -c echo 'myservice.service started' && echo $(date +%s) && sleep infinity
       └─5026 sleep infinity

I'm confused because my postinst is "small and dumb":

#!/bin/sh -e
# POSTINST script for myservice
set -e

echo "\033[36m HI I'M A POSTINST SCRIPT `date +"%s"` \033[39m"

Why does the presence of a postinst overwrite the "automatic enabling and starting" of a systemd service?

What part of debhelper deals with this and what am I missing?

Or what should I add to my postinst maintainer script to get my services up and running on install?


NOTES: My environment is a small vagrant box with a virtualbox provider. I am using the ubuntu-xenial64 Vagrant box. Not sure if this environment affects this.

Best Answer

You need to add the debhelper placeholder to your postinst:

#DEBHELPER#

So your postinst should look like

#!/bin/sh
# POSTINST script for intera
set -e

echo "\033[36m HI I'M A POSTINST SCRIPT `date +"%s"` \033[39m"

#DEBHELPER#

exit 0

If you don’t include that, debhelper won’t add its auto-generated postinst snippets to your maintainer script. When you don’t have a postinst at all, it just generates the entire script; but if you have your own version, it only changes the placeholder.