Running unattented systemd user service

In the process of learning how to deploy a Django webapp using Microsoft’s TFS build + release tools, I came across the need to convert several systemd services that used to be started by root  user to systemd user services, so that they could be started and managed by a non-privileged user. Here is one of my original services that is used to run gunicorn (*** designates values that have been redacted):

I ran these steps to convert the original service to a user service:

To my surprise, that worked well and the service has started without complaints. However, I soon discovered two problems:

  1. The service no longer started automatically after server reboot
  2. The service only ran when I was SSHed into the server as the user who now managed the service

Turns out, the fix for both issue is relatively straight-forward, once you know what to do, which is the hard part. In order to fix the first problem, I ended up changing the service file:

It turns out that such thing as multi-user.target does not exist for systemd user services, but default.target does.

The second issue was a bit more interesting. Roughly speaking, systemd only runs user services when a user is logged in. When the user logs out, their services are promptly stopped. In order to overcome that, you need to indicate to the login manager that user needs to linger. On Ubuntu 16.04, the command to do so is:

After applying both changes and rebooting, my systemd user services where chugging along happily without the need for manual intervention.