jtroo / kanata

Improve keyboard comfort and usability with advanced customization
GNU Lesser General Public License v3.0
2.11k stars 109 forks source link

Bug: Invalid quoting in systemd service file shown in docs #1153

Closed fluffychaos closed 1 week ago

fluffychaos commented 1 month ago

Requirements

Describe the bug

When using the systemd service file shown in setup-linux.md launching the service fails with sh complaining about -- being an invalid option. The error messages are shown in the service logs. When executing the command contained in ExecStart manually in zsh everything works. When looking at the command executed the ' quotes around the exec block are missing.

Relevant kanata config

kanata.service

[Unit]
Description=Kanata keyboard remapper
Documentation=https://github.com/jtroo/kanata

[Service]
Environment=PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin
Environment=DISPLAY=:0
Type=simple
ExecStart=/usr/bin/sh -c 'exec $$(which kanata) --cfg $${HOME}/.config/kanata/config.kbd'
Restart=no

[Install]
WantedBy=default.target

To Reproduce

  1. Copy-paste the content of the service file, shown in setup-linux.md, into the actual service file on the system
  2. systemctl --user daemon-reload
  3. systemctl --user start kanata.service
  4. systemctl --user status kanata.service

Expected behavior

Seeing Kanata starting normally in the logs

Kanata version

1.6.1

Debug logs

systemctl --user status kanata.service

kanata.service - Kanata keyboard remapper
     Loaded: loaded (/etc/systemd/user/kanata.service; enabled; preset: disabled)
    Drop-In: /usr/lib/systemd/user/service.d
             └─10-timeout-abort.conf
     Active: failed (Result: exit-code) since Wed 2024-07-24 15:22:31 CEST; 2s ago
   Duration: 5ms
       Docs: https://github.com/jtroo/kanata
    Process: 10671 ExecStart=/usr/bin/sh -c exec $$(which kanata) --cfg $${HOME}/.config/kanata/config.kbd (code=exited, status=2)
   Main PID: 10671 (code=exited, status=2)
        CPU: 5ms

/usr/bin/sh: line 1: exec: --: invalid option
exec: usage: exec [-cl] [-a name] [command [argument ...]] [redirection ...]
kanata.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
kanata.service: Failed with result 'exit-code'.

Operating system

Linux

Additional context

The concrete OS is Fedora 40 with systemd version 255.8-1.fc40

fluffychaos commented 1 month ago

I've also tried to escape the quotes with \' but this resulted in a different error

kanata.service - Kanata keyboard remapper
     Loaded: loaded (/etc/systemd/user/kanata.service; enabled; preset: disabled)
    Drop-In: /usr/lib/systemd/user/service.d
             └─10-timeout-abort.conf
     Active: failed (Result: exit-code) since Wed 2024-07-24 15:52:30 CEST; 3s ago
   Duration: 5ms
       Docs: https://github.com/jtroo/kanata
    Process: 11542 ExecStart=/usr/bin/sh -c 'exec $$(which kanata) --cfg $${HOME}/.config/kanata/config.kbd' (code=exited, status=2)
   Main PID: 11542 (code=exited, status=2)
        CPU: 4ms

Started kanata.service - Kanata keyboard remapper.
$(which: -c: line 1: unexpected EOF while looking for matching `''
kanata.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
kanata.service: Failed with result 'exit-code'.
jtroo commented 1 month ago

Interesting 🤔

Any improvement to the doc would be welcome! This has apparently worked for someone since they contributed the suggestion, but perhaps it doesn't work in all versions/configurations of systemd.

redmaw commented 1 month ago

In my case this error was caused by which not finding the executable in $HOME/.cargo/bin. Copying it to /usr/local/bin fixed the issue. Haven't taken the time to figure out the solution or the problem so cannot share any more details.

fluffychaos commented 1 week ago

I didn't have much time lately to look at this problem again, unfortunately, but thanks to the help on the systemd matrix room I now have a working solution not involving the shell at all.

[Unit]
Description=Kanata keyboard remapper
Documentation=https://github.com/jtroo/kanata

[Service]
Environment=PATH=%h/.cargo/bin
Environment=DISPLAY=:0
ExecSearchPath=%h/.cargo/bin
Type=simple
ExecStart=kanata --cfg %h/.config/kanata/kanata.kbd
Restart=no

[Install]
WantedBy=default.target

PATH and ExecSearchPath obviously needs to be adjusted if kanata was not installed via cargo but this executes kanata without the need for a shell exec. The %h is a systemd specifier for the home directory of the user running the current service manager. So if you are currently the user Bob and execute systemctl --user start kanata.service %h would resolve to /home/Bob

fluffychaos commented 1 week ago

I should note that, in my case, I also had to set ExecSearchPath in order for systemd to find kanata. Only setting PATH did not suffice for me.