# Help with udev rules

**URL:** <https://forum.endeavouros.com/t/help-with-udev-rules/36273>\
**Category:** Kernel, boot, graphics & hardware\
**Created:** [January 20, 2023, 10:33pm UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273 "2023-01-20T22:33:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaun\_snow](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/shaun_snow/32/79722_2.png) [@shaun\_snow](https://forum.endeavouros.com/u/shaun_snow)\
**Post date:** [January 20, 2023, 10:33pm UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/1 "2023-01-20T22:33:07Z")

</div>

hi, im trying to create my first udev rules. i googled a lot but i cant get it to work ☹

my approach was:

> [@udevadm monitor --environment --udev](#):
>
> UDEV [9587.233969] add /devices/pci0000:00/0000:00:08.1/0000:05:00.3/usb1/1-3/1-3:1.0/bluetooth/hci0/hci0:256/0005:045E:02E0.0010/input/input32 (input)  
> ACTION=add  
> DEVPATH=/devices/pci0000:00/0000:00:08.1/0000:05:00.3/usb1/1-3/1-3:1.0/bluetooth/hci0/hci0:256/0005:045E:02E0.0010/input/input32  
> SUBSYSTEM=input  
> PRODUCT=5/45e/2e0/903  
> NAME=“Xbox Wireless Controller”  
> PHYS=“cc:15:31:da:b9:60”  
> UNIQ=“98:7a:14:3d:18:b8”  
> PROP=0  
> EV=1b  
> KEY=3ff000000000000 0 800 0 0  
> ABS=3003f  
> MSC=10  
> MODALIAS=input:b0005v045Ep02E0e0903-e0,1,3,4,k8B,130,131,132,133,134,135,136,137,138,139,ra0,1,2,3,4,5,10,11,m4,lsfw  
> SEQNUM=4332  
> USEC\_INITIALIZED=9587233734  
> ID\_INPUT=1  
> ID\_INPUT\_JOYSTICK=1  
> ID\_INPUT\_KEY=1  
> ID\_BUS=bluetooth  
> ID\_PATH=pci-0000:05:00.3-usb-0:3:1.0  
> ID\_PATH\_TAG=pci-0000\_05\_00\_3-usb-0\_3\_1\_0  
> ID\_FOR\_SEAT=input-pci-0000\_05\_00\_3-usb-0\_3\_1\_0  
> TAGS=💺  
> CURRENT\_TAGS=💺

i created:

> [@/etc/udev/rules.d/50-bluetoothdevices.rules](#):
>
> ACTION==“add”, SUBSYSTEM==“input”, ATTRS{PHYS}==“cc:15:31:da:b9:60”, RUN+=“~/Scripts/xbox\_controller.sh”  
> ACTION==“remove”, SUBSYSTEM==“input”, ATTRS{PHYS}==“cc:15:31:da:b9:60”, RUN+=“~/Scripts/xbox\_controller.sh”

testing the rule gave me the following result:

> [@udevadm test /sys/subsystem/bluetooth/devices/hci0:256](#):
>
> This program is for debugging only, it does not run any program  
> specified by a RUN key. It may show incorrect results, because  
> some values may be different, or not available at a simulation run.
> 
> Trying to open “/etc/systemd/hwdb/hwdb.bin”…  
> Trying to open “/etc/udev/hwdb.bin”…  
> Trying to open “/usr/lib/systemd/hwdb/hwdb.bin”…  
> Trying to open “/usr/lib/udev/hwdb.bin”…  
> === trie on-disk ===  
> tool version: 252  
> file size: 12223125 bytes  
> header size 80 bytes  
> strings 2479237 bytes  
> nodes 9743808 bytes  
> Loading kernel module index.  
> Failed to read $container of PID 1, ignoring: Permission denied  
> Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy  
> Found container virtualization none.  
> Using default interface naming scheme ‘v252’.  
> Parsed configuration file “/usr/lib/systemd/network/99-default.link”  
> Parsed configuration file “/usr/lib/systemd/network/80-iwd.link”  
> Created link configuration context.  
> Reading rules file: /usr/lib/udev/rules.d/01-md-raid-creating.rules  
> Reading rules file: /usr/lib/udev/rules.d/10-dm.rules  
> Reading rules file: /usr/lib/udev/rules.d/11-dm-lvm.rules  
> Reading rules file: /usr/lib/udev/rules.d/13-dm-disk.rules  
> Reading rules file: /usr/lib/udev/rules.d/39-usbmuxd.rules  
> Reading rules file: /usr/lib/udev/rules.d/40-gphoto.rules  
> Reading rules file: /usr/lib/udev/rules.d/40-usb-media-players.rules  
> Reading rules file: /usr/lib/udev/rules.d/40-usb\_modeswitch.rules  
> Reading rules file: /etc/udev/rules.d/45-ddcutil-i2c.rules  
> Reading rules file: /usr/lib/udev/rules.d/45-i2c-tools.rules  
> Reading rules file: /etc/udev/rules.d/50-bluetoothdevices.rules  
> Reading rules file: /usr/lib/udev/rules.d/50-udev-default.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-autosuspend.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-block.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-cdrom\_id.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-drm.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-evdev.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-fido-id.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-input-id.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-persistent-alsa.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-persistent-input.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage-tape.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-persistent-v4l.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-rfkill.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-sensor.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-serial.rules  
> Reading rules file: /usr/lib/udev/rules.d/60-tpm-udev.rules  
> Reading rules file: /usr/lib/udev/rules.d/61-kde-bluetooth-rfkill.rules  
> Reading rules file: /usr/lib/udev/rules.d/63-md-raid-arrays.rules  
> Reading rules file: /usr/lib/udev/rules.d/64-btrfs-dm.rules  
> Reading rules file: /usr/lib/udev/rules.d/64-btrfs-zoned.rules  
> Reading rules file: /usr/lib/udev/rules.d/64-btrfs.rules  
> Reading rules file: /usr/lib/udev/rules.d/64-md-raid-assembly.rules  
> Reading rules file: /usr/lib/udev/rules.d/65-libwacom.rules  
> Reading rules file: /usr/lib/udev/rules.d/65-sane.rules  
> Reading rules file: /usr/lib/udev/rules.d/66-saned.rules  
> Reading rules file: /usr/lib/udev/rules.d/69-dm-lvm.rules  
> Reading rules file: /usr/lib/udev/rules.d/69-libftdi.rules  
> Reading rules file: /usr/lib/udev/rules.d/69-libmtp.rules  
> Reading rules file: /usr/lib/udev/rules.d/69-md-clustered-confirm-device.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-camera.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-infrared.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-joystick.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-memory.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-mouse.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-power-switch.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-steam-input.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-steam-vr.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-touchpad.rules  
> Reading rules file: /usr/lib/udev/rules.d/70-uaccess.rules  
> Reading rules file: /usr/lib/udev/rules.d/71-seat.rules  
> Reading rules file: /usr/lib/udev/rules.d/73-seat-late.rules  
> Reading rules file: /usr/lib/udev/rules.d/75-net-description.rules  
> Reading rules file: /usr/lib/udev/rules.d/75-probe\_mtd.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-broadmobi-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-cinterion-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-dell-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-dlink-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-ericsson-mbm.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-fibocom-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-foxconn-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-gosuncn-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-haier-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-huawei-net-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-linktop-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-longcheer-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-mtk-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-nokia-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-quectel-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-sierra.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-simtech-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-telit-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-tplink-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-ublox-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-x22x-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/77-mm-zte-port-types.rules  
> Reading rules file: /usr/lib/udev/rules.d/78-sound-card.rules  
> Reading rules file: /usr/lib/udev/rules.d/80-drivers.rules  
> Reading rules file: /usr/lib/udev/rules.d/80-libinput-device-groups.rules  
> Reading rules file: /usr/lib/udev/rules.d/80-mm-candidate.rules  
> Reading rules file: /usr/lib/udev/rules.d/80-net-setup-link.rules  
> Reading rules file: /usr/lib/udev/rules.d/80-udisks2.rules  
> Reading rules file: /usr/lib/udev/rules.d/81-net-dhcp.rules  
> Reading rules file: /usr/lib/udev/rules.d/84-nm-drivers.rules  
> Reading rules file: /usr/lib/udev/rules.d/85-nm-unmanaged.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-alsa-restore.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-daxctl-device.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-fwupd-devices.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-libgpod.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-libinput-fuzz-override.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-nm-thunderbolt.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-pipewire-alsa.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-udisks2-zram.rules  
> Reading rules file: /usr/lib/udev/rules.d/90-vconsole.rules  
> Reading rules file: /usr/lib/udev/rules.d/95-dm-notify.rules  
> Reading rules file: /usr/lib/udev/rules.d/95-upower-hid.rules  
> Reading rules file: /usr/lib/udev/rules.d/95-upower-wup.rules  
> Reading rules file: /usr/lib/udev/rules.d/96-e2scrub.rules  
> Reading rules file: /usr/lib/udev/rules.d/99-fuse.rules  
> Reading rules file: /usr/lib/udev/rules.d/99-fuse3.rules  
> Reading rules file: /usr/lib/udev/rules.d/99-nfs.rules  
> Reading rules file: /usr/lib/udev/rules.d/99-systemd.rules  
> sd-device: Failed to chase symlinks in “/sys/subsystem/bluetooth/devices/hci0:256”.  
> Failed to open device ‘/sys/subsystem/bluetooth/devices/hci0:256’: No such device  
> Unload kernel module index.  
> Unloaded link configuration context.

i suspect the last few lines to be relevant, however i dont know what to do with is. there is no “subsystem” folder in /sys/

* * *

another thing that i think could be related to my issue is, that ddcutil (_even though working as intended_) gives me the following error each time i use it. this was not the case with my previous distro, where i set up ddcutil the exact same way, following the ddcutil wiki.

> [@](#):
>
> Unable to open directory /sys/bus/i2c/devices/i2c–1: No such file or directory  
> Device /dev/i2c-255 does not exist. Error = ENOENT(2): No such file or directory  
> /sys/bus/i2c buses without /dev/i2c-N devices: /sys/bus/i2c/devices/i2c-255  
> Driver i2c\_dev must be loaded or builtin  
> See [https://www.ddcutil.com/kernel\_module](https://www.ddcutil.com/kernel_module)

---

<div class="post-metadata">

**Author:** ![jake99](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/jake99/32/69549_2.png) [@jake99](https://forum.endeavouros.com/u/jake99)\
**Post date:** [January 21, 2023, 3:06am UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/2 "2023-01-21T03:06:50Z")

</div>

For ddcutil try to load `i2c-dev` module first

```auto
sudo modprobe i2c-dev

```

For udev rule first try with `ATTRS{uniq}=="98:7a:14:3d:18:b8"` instead of `ATTRS{PHYS}...` and add `KERNEL=="js0"`

If that does not work, then try to get information from actual device not the subsytem (which should be hidraw, anyway, but input may work). I dont have the xbox controller but I guess it will create a device node `/dev/input/js0` for the controller itself dont use `/sys/subsystem/bluetooth....` as it might change dynamically.

So execute

```auto
udevadm info --attribute-walk --path=$(udevadm info --query=path --name=/dev/input/js0)

```

I suspect `RUN+=“~/Scripts/xbox_controller.sh”` part is problematic, too. What is the full path to your script? Is it in home folder? Udev is not running in interactive shell and the scripts will need to start with `#!/bin/sh` it will not run `bash` and will run as root.

Anyway I would do it like this:

1. Create a file `/etc/udev/rules.d/98-xboxcontroller.rules` with the following contents

```auto
ACTION=="add", KERNEL=="js0", SUBSYSTEM=="input", ATTRS{uniq}=="98:7a:14:3d:18:b8", RUN+="/usr/local/bin/udev-test-add.sh"
ACTION=="remove", KERNEL=="js0", SUBSYSTEM=="input", ATTRS{uniq}=="98:7a:14:3d:18:b8", RUN+="/usr/local/bin/udev-test-del.sh"

```

1. Create my run scripts in `/usr/local/bin` and `chmod +x` them with test contents like this for added script:

```auto
#!/bin/sh
touch /tmp/added-xbox-$(date +%s)

```

and removed script

```auto
#!/bin/sh
touch /tmp/removed-xbox-$(date +%s)

```

1. At last I would test them with

```auto
sudo udevadm test $(udevadm info --query=path --name=/dev/input/js0) 2>&1

```

and with turning controller on and off.

---

<div class="post-metadata">

**Author:** ![shaun\_snow](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/shaun_snow/32/79722_2.png) [@shaun\_snow](https://forum.endeavouros.com/u/shaun_snow)\
**Post date:** [January 21, 2023, 9:45am UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/3 "2023-01-21T09:45:37Z")

</div>

thanks for your help jake!

loading i2c-dev doesnt get rid of the error messages from ddcutil. since its working i dont really care too much though

* * *

for my udev rule i tried what you said, however it didnt work. but i DID get it to work addressing the xbox controller battery like this:

> [@](#):
>
> ACTION==“\*”, KERNEL==“hid-98:7a:14:3d:18:b8-battery”, SUBSYSTEM==“power\_supply”, RUN+=“/home/shaun\_snow/Scripts/xbox\_controller.sh”

the funny thing is ACTION==“remove” works, but ACTION==“add” doesnt. thats why right now it is \*, which triggers both events. do you have any idea if maybe for the battery the action isnt add but something else like read, initiate, etc?

* * *

also i noticed creating a file on desktop works, but launching programs dont. it doesnt matter if i use bin/bash or bin/sh, “touching” a file works with both but launching (for bin/sh i used “exec command”)

---

<div class="post-metadata">

**Author:** ![jake99](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/jake99/32/69549_2.png) [@jake99](https://forum.endeavouros.com/u/jake99)\
**Post date:** [January 21, 2023, 6:46pm UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/4 "2023-01-21T18:46:00Z")

</div>

> [@shaun\_snow](#):
>
> my udev rule i tried what you said, however it didnt work

Can you provide the full output of

```auto
udevadm info --attribute-walk --path=$(udevadm info --query=path --name=/dev/input/js0)

```

> [@shaun\_snow](#):
>
> but i DID get it to work addressing the xbox controller battery like this

Thats strange, notice the `uniq` value in that hid string 😃 I think there is a better way to specify the device, but whatever works 😃

> [@shaun\_snow](#):
>
> the funny thing is ACTION==“remove” works, but ACTION==“add” doesnt.  
> do you have any idea if maybe for the battery the action isnt add but something else

Check the execution order of your rules file. Rename it to be something like `99-xbox.rules`  
Action could be anything, but you can easily find out what it is; udev will set `ACTION` env variable when it executes your rule run script. Change your scripts to use `env` instead of `touch`

```auto
#!/bin/sh
env > /tmp/added-xbox-$(date +%s)

```

and check the variables in that file.

> [@shaun\_snow](#):
>
> creating a file on desktop works, but launching programs dont

Yeah, you can not launch GUI stuff like this from udev, even with `exec ... &` , also udev will kill any running script if it thinks it executes for too long. If you want to launch a GUI program it would be better to signal some other process or use systemd (maybe?)

---

<div class="post-metadata">

**Author:** ![shaun\_snow](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/shaun_snow/32/79722_2.png) [@shaun\_snow](https://forum.endeavouros.com/u/shaun_snow)\
**Post date:** [January 21, 2023, 7:28pm UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/5 "2023-01-21T19:28:22Z")

</div>

well i got it working so far, connecting my bluetooth keyboard (actual usecase, xbox controller was just testing) triggers the script

however, the only command that seems to be working is creating a dummy file on my desktop :-/

> [@](#):
>
> #!/bin/sh
> 
> touch /home/shaun\_snow/Desktop/test.txt  
> id=$(xinput list --short | grep i4 | grep keyboard | cut -c 53-55)  
> xinput set-prop “pointer:i4” “libinput Accel Speed” “-0.25”  
> xinput set-prop “pointer:i4” “libinput Middle Emulation Enabled” “1”  
> setxkbmap -device $id -option ctrl:ralt\_rctrl  
> kdialog --passivepopup “custom config loaded” 10 --title “bluetooth keyboard”

* * *

i dont know much about systemd, i used MX with sysvinit before EOS. i looked into it and it seems my udev rule can start a systemd service that will launch my script without killing it right away? i mean that sounds kinda weird but if it works

not sure how to accomplish yet, if you have any advice i will gladly take it 🙂

> [@my systemd-service](#):
>
> [Unit]  
> Description=i4
> 
> [Service]  
> Type=oneshot  
> User=shaun\_snow  
> ExecStart=/home/shaun\_snow/Scripts/bluetooth\_keyboard\_config.sh
> 
> [Install]  
> WantedBy=multi-user.target

> [@my udev rule](#):
>
> ACTION==“add”, SUBSYSTEMS==“usb”, ATTRS{idProduct}==“0029”, ATTRS{idVendor}==“8087”, RUN+=“/usr/bin/systemctl --no-block start i4.service”

> [@sudo systemctl status i4](#):
>
> × i4.service - i4  
> Loaded: loaded (/etc/systemd/system/i4.service; disabled; preset: disabled)  
> Active: failed (Result: exit-code) since Sat 2023-01-21 20:59:29 CET; 6min ago  
> Duration: 40ms  
> Main PID: 20581 (code=exited, status=1/FAILURE)  
> CPU: 44ms
> 
> Jan 21 20:59:29 localhost.localdomain systemd[1]: Started i4.  
> **Jan 21 20:59:29 localhost.localdomain bluetooth\_keyboard\_config.sh[20583]: Unable to connect to X server**  
> **Jan 21 20:59:29 localhost.localdomain bluetooth\_keyboard\_config.sh[20584]: Unable to connect to X server**  
> **Jan 21 20:59:29 localhost.localdomain bluetooth\_keyboard\_config.sh[20589]: Unable to connect to X server**  
> **Jan 21 20:59:29 localhost.localdomain bluetooth\_keyboard\_config.sh[20594]: Cannot open display “default display”**  
> **Jan 21 20:59:29 localhost.localdomain kdialog[20596]: qt.qpa.plugin: Could not load the Qt platform plugin “xcb” in “” even though it was found.**  
> **Jan 21 20:59:29 localhost.localdomain kdialog[20596]: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.**
> 
> Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.  
> Jan 21 20:59:29 localhost.localdomain systemd[1]: i4.service: Main process exited, code=exited, status=1/FAILURE  
> Jan 21 20:59:29 localhost.localdomain systemd[1]: i4.service: Failed with result ‘exit-code’.

* * *

i tried running _systemctl --user import-environment DISPLAY XAUTHORITY_ to get rid of the “cant connect to x server” and “cannot open display” errors. to be honest i dont really know what i did there, is this safe?

---

<div class="post-metadata">

**Author:** ![jake99](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/jake99/32/69549_2.png) [@jake99](https://forum.endeavouros.com/u/jake99)\
**Post date:** [January 23, 2023, 2:53am UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/6 "2023-01-23T02:53:41Z")

</div>

Can you describe your usecase? What are you trying to do exactly when your bluetooth keyboard is connected? Configure it like you posted in your previous script and show the notification? There are better ways than `udev` for UI handling. You can monitor Xs’ input events to configure the keyboard and run your scripts.

**Inputplug** is one popular solution [https://github.com/andrewshadura/inputplug](https://github.com/andrewshadura/inputplug)

> [@](#):
>
> inputplug is a very simple daemon which monitors XInput events and runs arbitrary scripts on hierarchy change events (such as a device being attached, removed, enabled or disabled).

Systemd is the “cleanest” solution, but it can be complicated to setup. Basically udev should signal systemd socket, that will then activate systemd service that will run your script in user context. All other udev solutions are “hacky” and are _not_ secure especially because **udev is running as root** and in **non-interactive shell**.

All that said, I will show you a way how to do this from a quick and dirty script with kdialog, even if I **don’t** recommend doing this. Remember, you are running GUI app as root in user context, it can mess a lot of things up.

Besides `DISPLAY` and `XAUTHORITY` env vars, you will need `XDG_RUNTIME_DIR` and `QT_QPA_PLATFORM` set if you need any KDE app to run from this context, so your script may look like this:

```auto
#!/bin/sh

# use `ps` to find running Xwayland process, it will return something like this
# /usr/bin/Xwayland :1 -auth /run/user/1000/xauth_ifAwgP -listen 57 -listen 58 -displayfd 49 -rootless -wm 52
# on my system, adapt to different process if using X11
#
# there we have DISPLAY, XAUTHORITY, XDG_RUNTIME_DIR in the returned command line    
MY_RUNNING_X_PS="$(ps -A -o cmd= | grep Xwayland | grep -v grep)" 

export DISPLAY="$(echo ${MY_RUNNING_X_PS} | cut -d ' ' -f 2)" # :1 
export XAUTHORITY="$(echo ${MY_RUNNING_X_PS} | cut -d ' ' -f 4)" # /run/user/1000/xauth....
export XDG_RUNTIME_DIR="/run/user/1000" # this can be extracted too, but left as exercise      
export QT_QPA_PLATFORM="wayland" # put xcb here if X11
export WAYLAND_DISPLAY="wayland-0" # don't have/need this env on X11
                                                                                               
env > /tmp/myevent-$(date +%s) && kdialog --passivepopup "custom config loaded" 10 --title "bluetooth keyboard ${ACTION}"

```

---

<div class="post-metadata">

**Author:** ![shaun\_snow](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/shaun_snow/32/79722_2.png) [@shaun\_snow](https://forum.endeavouros.com/u/shaun_snow)\
**Post date:** [January 23, 2023, 5:20pm UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/7 "2023-01-23T17:20:38Z")

</div>

my usecase is the script i posted. the notification is just there to indicate the script has worked, i just need the 2 xinput + setxkbmap commands to configure my bluetooth keyboard. the keyboard goes to sleep after 5 minutes, after waking up all custom settings are forgotten and i need to reset them somehow.

however, i would really like to be able to use udev rules for other usecases as well. for instance start my backup script, whenever i plug in my externel backup drive or start steam, whenever i connect my xbox controller. but the important thing is the bluetooth keyboard.

i dont wanna use a “hacky” solution, especially if it brings security risks! so i got that right, udev runs as root, even if it triggers a systemd service that starts my script? … and thats why its so hard to use udev to start commands that rely on X server?

im down to use any solution thats safe. i will take a look at inputplug, thanks for the suggestion. also thank you for writing the script for me, but as i already said: i care about security and if you advise not to do it that way i wont.

im really willing to learn, thats the fun part about linux.

i would appreciate if you could answer my question at the end of my last post regarding the imported environment variables. is this a permanent change i did to my system and how can i revert it? as much as i want my problem to be solved i want to have a clean and safe system. i did this out of desperation to be honest 😖

---- edit ----

how i understand it udev and the program you suggested (inputplug) both are daemons (?), that continiously run in the background and watch for changes. they probably update quite often, to get the information quickly. my existing udev rule can create a dummy file, so why not write my own daemon? something like

> [@](#):
>
> n=0  
> while [$n != 1]  
> do  
> if [-f dummy.txt]  
> then  
> _my commands to configure the bluetooth keyboard_  
> fi  
> sleep 1  
> done

---

<div class="post-metadata">

**Author:** ![jake99](https://forum.endeavouros.com/user_avatar/forum.endeavouros.com/jake99/32/69549_2.png) [@jake99](https://forum.endeavouros.com/u/jake99)\
**Post date:** [January 24, 2023, 11:08pm UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/8 "2023-01-24T23:08:46Z")

</div>

> [@shaun\_snow](#):
>
> i would appreciate if you could answer my question at the end of my last post regarding the imported environment variables. is this a permanent change i did to my system and how can i revert it?

Not a big issue, disable/delete the service you created, and thats it, no permanent damage to the system

> [@shaun\_snow](#):
>
> how i understand it udev and the program you suggested (inputplug) both are daemons (?), that continiously run in the background and watch for changes. they probably update quite often, to get the information quickly

Yes, more or less. The big difference is that _udev_ is a linux **subsystem** and operates on lower level than _inputplug_ **daemon** which “listens” for Xorg input events.

> [@shaun\_snow](#):
>
> so why not write my own daemon?

Sure, you can do that, just make sure the “daemon” is running in user context.

---

<div class="post-metadata">

**Author:** ![system](https://forum.endeavouros.com/uploads/default/original/2X/9/9ea52f9fd5ef8e609e593c362d61a36295f48be8.png) [@system](https://forum.endeavouros.com/u/system)\
**Post date:** [January 26, 2023, 11:08pm UTC](https://forum.endeavouros.com/t/help-with-udev-rules/36273/9 "2023-01-26T23:08:47Z")

</div>

Closed on January 26, 2023, 11:08pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
