I have a startup script that sometimes runs on startup and sometimes doesn't (I don't know why). This is a problem for me because the script swaps keys on my keyboard so I need the keys swapped after my desktop loads. I wrote a systemd service but it won't work. Here's my service file.
# /etc/systemd/system/keySwap.service
[Unit]
Description=Swaps right Option key with Right CMD key
After=display-manager.service
[Service]
ExecStart=/home/jedrek/keyboard_keys_swapper.sh
[Install]
WantedBy=default.target
When I run the service manually in terminal in graphical environment it errors out.
sudo systemctl start keySwap
sudo systemctl status keySwap
● keySwap.service - Swaps right Option key with Right CMD key
Loaded: loaded (/etc/systemd/system/keySwap.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2022-04-05 18:57:24 CEST; 4s ago
Process: 12028 ExecStart=/home/jedrek/keyboard_keys_swapper.sh (code=exited, status=1/FAILURE)
Main PID: 12028 (code=exited, status=1/FAILURE)
kwi 05 18:57:24 Home systemd[1]: Started Swaps right Option key with Right CMD key.
kwi 05 18:57:24 Home keyboard_keys_swapper.sh[12029]: xmodmap: unable to open display ''
kwi 05 18:57:24 Home keyboard_keys_swapper.sh[12030]: xmodmap: unable to open display ''
kwi 05 18:57:24 Home systemd[1]: keySwap.service: Main process exited, code=exited, status=1/FAILURE
kwi 05 18:57:24 Home systemd[1]: keySwap.service: Failed with result 'exit-code'.
The shell script simply swaps the keys:
#!/bin/bash
xmodmap -e "keycode 108 = Super_R"
xmodmap -e "keycode 134 = ISO_Level3_Shift"
I know that xmodmap requires graphical desktop to be loaded so I don't understand why it doesn't work. How can I solve my problem?