bgenc.net/content/posts/running-graphical-user-services-with-systemd.md

4.4 KiB

title date
Running graphical user services with systemd 2022-03-18

This post is day 3 of me taking part in the #100DaysToOffload challenge.

I've recently switched from KDE Plasma to sway as my window manager. I had a problem with the change though: the amazing kdeconnect service weren't working!

My first attempt at fixing this was to just add a lines into sway config to launch it along with sway.

exec /usr/lib/kdeconnectd

Looks simple enough. But for some reason, kdeconnectd would just disappear after a while. It would appear to run at startup, and then an hour or two later I pull up the kdeconnect app on my phone and it would tell me that my computer is disconnected.

The biggest issue here was that I had no way to see why kdeconnect had failed. In comes systemd to save the day. Systemd is a service manager, so it will actually maintain the logs for these services. That means if kdeconnect is crashing, I can check the logs for kdeconnect to see why it crashed. I can also configure it to auto-restart after a crash if I want to.

To launch graphical applications with systemd though, you need to pass the appropriate environment variables to it so it knows how to launch new windows. I added this line to my sway config to do exactly that.

# Pass all variables to dbus & systemd to run graphical user services
exec dbus-update-activation-environment --all --systemd

Next, we need to write a service files to run the application. This is easier than it sounds, here's the service file I wrote for kdeconnect:

[Unit]
Description=Run kdeconnectd.
After=graphical-session.target
StartLimitIntervalSec=600
StartLimitBurst=5

[Service]
Type=basic
ExecStart=/usr/lib/kdeconnectd
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=graphical-session.target

I saved this as ~/.config/systemd/user/kdeconnectd.service. Finally, enabled it for my user with systemctl --user enable kdeconnectd.service and then restarted.

The service is configured to automatically restart on failure, but not if it failed more than 5 times in the last 10 minutes. Systemd also waits 5 seconds before trying to restart the failed service. This way if it crashes for some reason, it will restart. But if it keeps crashing rapidly, it won't keep trying to restart which could take up too much system resources.

I can now check how the service is doing with systemd!

Warning: The unit file, source configuration file or drop-ins of kdeconnectd.service changed on disk. Run 'systemctl --user daemon-reload>
● kdeconnectd.service - Run kdeconnectd.
     Loaded: loaded (/home/kaan/.config/systemd/user/kdeconnectd.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-03-17 14:18:15 EDT; 1h 46min ago
   Main PID: 2188363 (kdeconnectd)
      Tasks: 6 (limit: 77007)
     Memory: 24.2M
        CPU: 2.440s
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/kdeconnectd.service
             └─2188363 /usr/lib/kdeconnectd

Mar 17 14:20:58 eclipse systemd[817]: /home/kaan/.config/systemd/user/kdeconnectd.service:6: Unknown key name 'type' in section 'Service'>
Mar 17 15:16:11 eclipse kdeconnectd[2188363]: QObject::connect(KWayland::Client::Registry, Unknown): invalid nullptr parameter
Mar 17 15:16:11 eclipse kdeconnectd[2188363]: kdeconnect.plugin.battery: No Primary Battery detected on this system. This may be a bug.
Mar 17 15:16:11 eclipse kdeconnectd[2188363]: kdeconnect.plugin.battery: Total quantity of batteries found:  0
Mar 17 15:23:26 eclipse kdeconnectd[2188363]: QObject::connect(KWayland::Client::Registry, Unknown): invalid nullptr parameter
Mar 17 15:23:26 eclipse kdeconnectd[2188363]: kdeconnect.plugin.battery: No Primary Battery detected on this system. This may be a bug.
Mar 17 15:23:26 eclipse kdeconnectd[2188363]: kdeconnect.plugin.battery: Total quantity of batteries found:  0
Mar 17 15:23:26 eclipse kdeconnectd[2188363]: QMetaObject::invokeMethod: No such method KIO::StoredTransferJob::slotDataReqFromDevice()
Mar 17 15:24:35 eclipse kdeconnectd[2188363]: QMetaObject::invokeMethod: No such method KIO::StoredTransferJob::slotDataReqFromDevice()
Mar 17 15:57:29 eclipse systemd[817]: /home/kaan/.config/systemd/user/kdeconnectd.service:9: Unknown key name 'type' in section 'Service'>

A bunch of warnings so far, but no crashes yet. But if it does crash again, I'll finally know why.