systemd: enabling a user-unit service
In my experience, setting up a systemd service as a user unit can be trickier than setting it up as a system unit.
You need to pay attention to a few key points to get it working correctly.
TL;DR
At first, here is my service file.
[Unit] Description=voice from pushbullet #Wants=default.target After=network.target [Service] Type=simple WorkingDirectory=%h/src/pb-va ExecStart=%h/bin/pb_va some options... ExecStop=pkill pb-va Restart=always [Install] WantedBy=default.target
Copy this file to the systemd directory and enable it::
$ mkdir -p ~/.config/systemd/user $ cp pb-va.service ~/.config/systemd/user $ systemctl --user daemon-reload $ systemctl --user enable pb-va.service $ systemctl --user start pb-va.service $ loginctl enable-linger $USER
Do not specify the User=
and Group=
options
In the [Service]
section,
system units can use the User=
and Group=
options.
However, user units run under your own user account by default, so adding these options will cause the service to fail on startup.
Use User-Specific Targets like default.target
You might be used to seeing targets like multi-user.target in system-wide services.
Those targets are for system units and won't work for user units. For user services, you should almost always use WantedBy=default.target.
This is the user's equivalent and ensures your service starts after you log in.
Remember to re-enable the service and reload the daemon after changing the serfice file to apply your changes:
$ systemctl --user daemon-reload $ systemctl --user disable pb-va.service $ systemctl --user enable pb-va.service
Run Services Even When Logged Out (Enable Linger)
By default, user services stop when you log out.
If you want your service to start at boot and keep running, you must enable linger for your user.
$ loginctl enable-linger your-username # then confirm the linger status. $ loginctl list-users UID USER LINGER 1001 your-username yes