Start a systemd user service at boot
I am trying to start a systemd service for a specific user (service name/goal is not relevant). I do that because I need to have a per user process of an application running at startup.
Here is what I achieved so far:
-
I create a unit file in /etc/systemd/user/
-
And then did the following:
~ $ systemctl --user enable custom.service
-
Then as said in https://wiki.archlinux.org/index.php/Systemd/User#Automatic_start-up_of_systemd_user_instances enabled lingering to have said service running even with no user session running.
~ $ loginctl enable-linger $USER
But when I reboot the service does not seem to start and there is nothing to be seen in journalctl for said service. But the status said that the service is enabled:
~ $ systemctl --user status transmission-daemon
● custom.service - Custom Daemon
Loaded: loaded (/etc/systemd/user/custom.service; enabled; vendor preset: enabled)Active: inactive (dead)
Also I can run it manually with no issue by running
~ $ systemctl --user start custom.service
As anyone any idea what I did wrong?
Solution 1:
I dont know if this is your case but i could solve it with the answer of https://unix.stackexchange.com/questions/251211/why-doesnt-my-systemd-user-unit-start-at-boot The problem was i had WantedBy=multi-user.target
and had to change it to WantedBy=default.target
and it worked.
Another thing, i have my service file in ~/.config/systemd/user
Solution 2:
The only thing necessary, from what I've seen, is to enable the service:
~ $ systemctl --user enable custom.service
and you said you've done that.
One way to check whether the start happened on reboot is to verify that there are no errors. You do that using the journalctl
command:
~ $ journalctl --user -u custom.service
Note: the -u
option stands for unit.
If nothing appears in there, you may have an invalid dependency, that is, the "wants" for user is default.target
. In your .service file it should look like so:
[Install]
WantedBy=default.target
Other targets are not likely to work and the auto-start will fail. That being said, the user has the targets listed by:
~ $ systemctl --user list-units --type=target
That does not include the multiuser, xsession, etc.
Finally, the X11 environment should be ready once the service starts, but I'm not 100% sure about that. In my .service file I also have an Environment=
definition that goes like this:
[Service]
Type=simple
Environment="DISPLAY=:0"
...snip...
I had problems where the service could not open an X-Window. With that small addition, it worked as expected.
One last thing, the User=...
and Group=...
parameters can't be used in a user service. Since it is specific to a user, you can't hope to use a specific user to run that application. Plus multiple users could be logged in the same computer and each need their own version of the service running in parallel. So other options may not be available to a user service. I would suggest you comment out most and then add one at a time to see what works and what doesn't in your situation. These errors, though, are the ones you'll see in the journal, so it should be relatively easy to fix once you bypass the few other steps.
Post Scriptum
The path where you want to save a User Service is:
~/.config/systemd/user/my-service.service
unless you want the service accessible to all users on that computer in which case you use:
/usr/lib/systemd/user/my-service.service
See this archlinux page for other details.
Note that I do not think that was your problem (wrong location) although it could have been. I have had difficulties before when I placed systemd files in the wrong place.