systemd user service not started after erboot

I cannot get user units to start automatically after reboot.

1: I’ve created a user unit file /home/test/.config/systemd/user/test.service (essentially a “sleep 3600”)

2: Reloaded the daemon “systemctl --user daemon-reload”

3: Enabled the service. Although it produced a weird error message:systemctl --user enable test
Failed to enable unit: File /home/test/.config/systemd/user/test.service already exists.

4: Start/stop of service works

5: Reboot machine

User service is not started. The processes systemd and (sd-pam) are running but not the service. Start/stop of service works.

What am I doing wrong? The goal is to have the user service automatically start after a reboot.

I don’t like the error messages I get in step but all the guides I’ve found points to that file location for the service file, and aside from not starting after reboot it seems to work.

Hi and welcome to the Forum :slight_smile:
Did you add a WantedBy= entry in your service? also did you run loginctl enable-linger USERNAME as well to enable at boot rather than login.

Yes, the unit file has WantedBy=multi-user.target

Yes, I did a “loginctl enable-linger test” - I forgot to list that in my original post. That seems to start systemd and (sd-pam) as it should after reboot, but from there on the user systemd seems to be unhappy about something as it doesn’t start the service automatically.

Hi
What does the output from the following show;


systemctl --user list-units --type=target

I would suggest changing to default.target and try again…

test@isjsys4:~> systemctl --user list-units --type=target
UNIT           LOAD   ACTIVE SUB    DESCRIPTION                                                                                                          
basic.target   loaded active active Basic System                                                                                                         
default.target loaded active active Default                                                                                                              
paths.target   loaded active active Paths                                                                                                                
sockets.target loaded active active Sockets                                                                                                              
timers.target  loaded active active Timers                                                                                                               

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

5 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

I tried that now, but it made no differences. Still only systemd and sd-pam is running for that user.

I found the problem.

If the unit file in its [Install] section has an Alias that happens to be the same as the unit file name (sans .service) then while enabling the service this unhelpful warning message appears:

Failed to enable unit: File /home/test/.config/systemd/user/test.service already exists.

and the unit/service is halfway loaded. It can be started, stopped, restarted, status queried, etc, but after reboot it isn’t automatically started.

Hi
Excellent :slight_smile: So all good and working now?

In case of trouble always search for all unit files, e.g.:

**erlangen:~ #** systemctl list-unit-files '*network*'              
UNIT FILE                             STATE    VENDOR PRESET
dbus-org.freedesktop.network1.service **alias    **-            
network.service                       **alias    **-            
systemd-network-generator.service     **disabled disabled     **
systemd-networkd-wait-online.service  **disabled disabled     **
[FONT=monospace][FONT=monospace]systemd-networkd.service              **enabled ****disabled     **
systemd-networkd.socket               **enabled ****disabled     **
[/FONT][/FONT]network-online.target                 static   -            
network-pre.target                    static   -            
network.target                        static   -  

9 unit files listed. 
**erlangen:~ #**

Apparently so.

There was actually more to it than that.

The target “multi-user.target” works for system units but not for user services. The target “default.target” works for user services.

So the unit file ended up looking like this:


[Unit]
Description=A nice description
[Service]
ExecStart=/full/path/to/a/program
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target