3

I want the user service nfancurve.service to start running every time I log in. I previously enabled the service using the command systemctl --user enable nfancurve.service, however, every time I start a new session (including reboots) the service isn't running, although it's enabled, which I verified by running these commands after logging in:

name@machine:~$ systemctl --user is-active nfancurve.service
inactive
name@machine:~$ systemctl --user is-enabled nfancurve.service
enabled
name@machine:~$ systemctl --user start nfancurve.service
name@machine:~$ systemctl --user is-active nfancurve.service
active

This is the status log from systemctl --user status nfancurve.service:

● nfancurve.service - Nfancurve service
     Loaded: loaded (/usr/lib/systemd/user/nfancurve.service; enabled; vendor preset: enabled)
     Active: inactive (dead)

For full information, the service comes from this repository, which I installed from the AUR. The .service file looks like this:

[Unit]
Description=Nfancurve service
After=graphical-session.target
Requires=graphical-session.target

[Service]
ExecStart=/bin/sh /usr/bin/nfancurve -c /etc/nfancurve.conf
KillSignal=SIGINT

[Install]
WantedBy=graphical-session.target

I already tried changing the last line to WantedBy=default.target, but after rebooting and logging in the service still didn't start. My desktop environment is xfce, in case this is relevant considering the graphical-session.target.

How do I make sure it automatically starts up at every login?

7
  • 1
    What do the logs say? After you boot the system and the service isn't started, what is the output of systemctl status nfancurve? Add all of that to your question. – Nasir Riley Jul 11 '20 at 21:23
  • I added the output of systemctl --user status nfancurve.service, since it's a user service. systemctl status nfancurve outputs Unit nfancurve.service could not be found.. – Wout12345 Jul 12 '20 at 7:21
  • If you systemctl --user cat nfancurve.service, What's in the [Install] section of the unit? – Stewart Jul 12 '20 at 7:41
  • @fra-san It's from that project indeed, I installed it using the AUR and I just verified that /usr/lib/systemd/user/nfancurve.service (the unit file, I assume?) matches nfancurve.service from that repository. I'll add the information about the package and installation to my question. – Wout12345 Jul 12 '20 at 17:19
  • @Stewart It's WantedBy=graphical-session.target. – Wout12345 Jul 12 '20 at 17:21
2

tl;dr

Change WantedBy=graphical-session.target to WantedBy=default.target.

Hypothesis

I suspect your problem is with the [Install] section of your service file. Installing it based on multi-user.target might be quite normal for system units, but the equivalent for user-units is default.target. See the systemd.special(7) manpage for details.

| system target     | near-equivalent user target |
|-------------------|-----------------------------|
| multi-user.target | default.target              |
| graphical.target  | graphical-session.target    |

Other targets that may also be interesting to you are graphical-session-pre.target or xdg-desktop-autostart.target.

default.target MVCE

Here's a minimal example I just tried on my Debian 11 machine:

The service file:

$ systemctl --user cat simpleuser.service
# /home/stew/.config/systemd/user/simpleuser.service
[Unit]
Description=Simple User Service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true

[Install]
WantedBy=default.target

I installed it with:

$ systemctl --user enable simpleuser.service
Created symlink /home/stew/.config/systemd/user/default.target.wants/simpleuser.service → /home/stew/.config/systemd/user/simpleuser.service.

I performed a reboot and I can see that the service is active:

$ systemctl --user status simpleuser.service
● simpleuser.service - Simple User Service
     Loaded: loaded (/home/stew/.config/systemd/user/simpleuser.service; enabled; vendor preset: enable>
     Active: active (exited) since Sun 2020-07-12 10:12:54 CEST; 13min left
    Process: 1127 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1127 (code=exited, status=0/SUCCESS)

Jul 12 10:12:54 stewbian systemd[1106]: Starting Simple User Service...
Jul 12 10:12:54 stewbian systemd[1106]: Finished Simple User Service.

If you're wondering about the (exited) substate: If you use something like Type=simple and ExecStart=/bin/sleep infinity, then you'll get (running) instead.

graphical-session.target problems

I found your question in the form of a bug report on github.

If you're installing based on graphical-session.target, then it's possible that isn't starting. I just ran this on my KDE/i3 setup and reproduced on a vanilla Gnome3 setup.

$ systemctl --user status graphical-session.target
● graphical-session.target - Current graphical user session
     Loaded: loaded (/usr/lib/systemd/user/graphical-session.target; static; vendor preset: disabled)
     Active: inactive (dead)
       Docs: man:systemd.special(7)

It sounds like DEs don't really implement this target per its design. In that case, you may be better-off using WantedBy=default.target. Your script doesn't look GUI-based so I bet default.target would be fine. Otherwise, you could consider using After=graphical.target in your [Unit] section.

According to systemd.special(7) manpage, graphical-session.target should be BoundBy= the DE (i.e. {gnome,kde,xfce}-session.target). This way, when Gnome starts, it will raise graphical-session.target. When I inspect the dbus objects I clearly see no relationships with any target which would raise it.

$ busctl introspect --user org.freedesktop.systemd1 \
    /org/freedesktop/systemd1/unit/graphical_2dsession_2etarget \
    org.freedesktop.systemd1.Unit
NAME                             TYPE      SIGNATURE RESULT/VALUE                             FLAGS
.ActiveState                     property  s         "inactive"                               emits-change
.After                           property  as        2 "simpleuser.service" "basic.target"    const
.Before                          property  as        1 "shutdown.target"                      const
.BindsTo                         property  as        0                                        const
.BoundBy                         property  as        0                                        const
.Conflicts                       property  as        1 "shutdown.target"                      const
.ConsistsOf                      property  as        2 "gnome-terminal-server.service" "duns… const
.Description                     property  s         "Current graphical user session"         const
.Documentation                   property  as        1 "man:systemd.special(7)"               const
.LoadState                       property  s         "loaded"                                 const
.Names                           property  as        1 "graphical-session.target"             const
.PartOf                          property  as        0                                        const
.RequiredBy                      property  as        0                                        const
.Requires                        property  as        1 "basic.target"                         const
.SubState                        property  s         "dead"                                   emits-change
.UnitFilePreset                  property  s         "disabled"                               -
.UnitFileState                   property  s         "static"                                 -
.WantedBy                        property  as        0                                        const
.Wants                           property  as        1 "simpleuser.service"                   const

I did some digging into the mailing lists and found that {gnome,kde,xfce}-session.target was proposed only four years ago with the intention that gnome, kde, xfce would ship the *.target files. It hasn't happened (yet). Therefore, even though it's documented, it's incomplete. Therefore you shouldn't use graphical-session.target yet.

2
  • Thanks for the extensive answer. I changed the [Install] section following your suggestion, but the problem persists. However, I also found the lines After=graphical-session.target and Requires=graphical-session.target in the [Unit] section, should these be modified too? – Wout12345 Jul 12 '20 at 17:44
  • Yes, Though, I think you can delete Requires=. Also, I think you can change After=graphical-session.target to After=graphical.target if you really want to prevent the script from running before the desktop environment starts. – Stewart Jul 12 '20 at 20:20
0

Your .service file should look like this:

[Unit]
Description=Spark service

[Service]
ExecStart=/path/to/spark/sbin/start-all.sh

[Install]
WantedBy=multi-user.target

Now, take a few more steps to enable and use the .service file:

  1. Place it in /etc/systemd/system folder with say a name of myfirst.service
  2. Make sure that your script executable with:
chmod u+x /path/to/spark/sbin/start-all.sh
  3. Start it:
sudo systemctl start myfirst
  4. Enable it to run at boot:
sudo systemctl enable myfirst
  5. Stop it:
sudo systemctl stop myfirst
1
  • multi-user.target is only applicable for system-wide services. OP is specifically asking about user services. – Stewart Jul 12 '20 at 17:13
0

I finally found a solution (thanks to Stewart and others). I changed the original service file:

[Unit]
Description=Nfancurve service
After=graphical-session.target
Requires=graphical-session.target

[Service]
ExecStart=/bin/sh /usr/bin/nfancurve -c /etc/nfancurve.conf
KillSignal=SIGINT

[Install]
WantedBy=graphical-session.target

by removing the Requires-line and replacing all instances of graphical-session.target by default.target(both the After and WantedBy lines). This leads to my current service file:

[Unit]
Description=Nfancurve service
After=default.target

[Service]
ExecStart=/bin/sh /usr/bin/nfancurve -c /etc/nfancurve.conf
KillSignal=SIGINT

[Install]
WantedBy=default.target

Now the service is active when I start my machine and log in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.