sxhkd: sxhkd not starting as a systemd service

I’m trying to start sxhkd as a systemd service. I started by following the Arch Wiki and used the example file provided, but I can’t make it work.

There is an ongoing discussion on the subject on Arch forum.

What we tried so far:

  • First, the systemd service unit must be started as a user, not as root.

The unit must be copied in the /etc/systemd/user/ folder and it must be enabled using systemctl --user enable sxhkd.service (no sudo!)

  • Then the following lines are not necessary anymore
BindsTo=display-manager.service
After=display-manager.service

Because the display-manager service is not visible by the user, and anyway, when the user session starts, we are past this target.

  • The WantedBy instruction must also be changed to:

WantedBy=default.target

because there is no grapical target visible by the user-level unit

Still, sxhkd crashes upon startup (see https://imgur.com/a/q46tI76 ). I can successfully start it manually afterwards however.

I’m stuck here because I don’t really know why sxhkd has crashed. Is there a way to have a log file that would explain why it crashes?

About this issue

Most upvoted comments

I got a working solution on my Arch. In first I create ~/.config/systemd/user/sxhkd.service file by following lines:

[Unit]
Description=Simple X Hotkey Daemon
Documentation=man:sxhkd(1)
PartOf=graphical-session.target
After=graphical-session.target

[Service]
Restart=always
RestartSec=2
ExecStart=/usr/bin/sxhkd
ExecReload=/usr/bin/kill -SIGUSR1 $MAINPID

[Install]
WantedBy=default.target

After it, I enable this service:

systemctl --user enable sxhkd.service

and reboot. So, it works:

❯ systemctl --user status sxhkd.service 
● sxhkd.service - Simple X Hotkey Daemon
     Loaded: loaded (/home/vagrant/.config/systemd/user/sxhkd.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-09-15 02:55:16 MSK; 13s ago
       Docs: man:sxhkd(1)
   Main PID: 483 (sxhkd)
     CGroup: /user.slice/user-1000.slice/user@1000.service/sxhkd.service
             └─483 /usr/bin/sxhkd

Note: if you got error sxhkd.service: Start request repeated too quickly., try to increase RestartSec option (2s on my example)

It’s not that it overrides other environment variables, it’s that there are no other environment variables.

I discovered systemctl --user show-environment, and you’re completely right!

Try without the Environment=. systemd will run the program without any environment variables not specifically set up in the .service file.

Yeah, but at point systemd tries to use SHELL (which it somehow guesses is fish) in conjunction with sxhkd. fish breaks parts of my sxhkd config, and is possibly slower than other shells.

In the end, I just added my prefered set of environment variables to ~/.config/environment.d/XYZ.conf, as outlined on the arch wiki , which was then reflected in systemctl --user show-environment. As such, I didn’t really have to add an Environment= line to the service, but could just override SHELL in said .conf file. sxhkd finally runs fine as systemd service!

@rien333 @shimarulin Adding KillMode=process to [Service] should fix that problem!

FYI you can place user units in ~/.config/systemd/user/ too. That’s how I have it set up.

As for the actual issue, I’m using some other hacks to run it as an service 😉

systemctl --user restart sxhkd

Is what I got in my bspwmrc. Everytime bspwm is started/restarted, so is sxhkd. This way though, sxhkd.service is never enabled. I’ve also got more units set up like this (oof). I know it’s not really “how you’re supposed to do it” but I never really cared enough to solve it the “right” way by enabling them and setting up display manager targets and stuff.

Okay but to actually tackle the issue: My guess is that sxhkd is starting before Xorg and this makes sxhkd crash. Can you get more output from sxhkd by doing journalctl --user --boot -u sxhkd?