37

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:

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?

Alexis Wilke
  • 1,806
Jesus_21
  • 471

3 Answers3

43

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

Ben
  • 3
The SWE
  • 531
12

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.

Alexis Wilke
  • 1,806
1

As you mentioned transmission-daemon, your other procedure seems good. I bet your service file has some problems. Because I created service files easily except for transmission-daemon.

Configs of each service file are somehow case by case. Of course If there is a service generator like podman generate systemd then, use it.

This is my transmission service file. If you have a mount file for it, then add it to After. e.g After=network-online.target mountfilename.mount

[Unit]
Description=Transmission BitTorrent Daemon
After=network-online.target

[Service] User=username Group=username Type=simple ExecStart=/usr/local/bin/transmission-daemon --config-dir /path/to/config/transmission-daemon -f --log-error ExecReload=/bin/kill -s HUP $MAINPID NoNewPrivileges=true

[Install] WantedBy=multi-user.target

This is for $ systemctl enable transmission-daemon, not --user option.

WantedBy=multi-user.target is because of my setting. I recommend WantedBy=default.target too.

For systemctl --user enable,

[Unit]
Description=Transmission BitTorrent Daemon
After=network-online.target

[Service] Type=simple ExecStart=/usr/local/bin/transmission-daemon --config-dir /path/to/config/transmission-daemon -f --log-error ExecReload=/bin/kill -s HUP $MAINPID NoNewPrivileges=true

[Install] WantedBy=default.target

But in this case, you can't put the service file /etc/systemd/user. Put it into ~/.config/systemd/user/. and enable it.