How to run a program as the current user when usb device inserted?

it still did not work.

@40476 You have to be a little bit more explicit, there are two actions, one the udev rule and two, the start/stop script… Remember I’m not the one in front of your computer…

So, modify the script as follows;

#!/usr/bin/bash

case "$1" in
  start)
    echo "Starting G510S daemon..."
    ##/usr/bin/g510s &
    echo 'Keyboard plug event' | systemd-cat
    ;;
  stop)
    echo "Stopping G510S daemon..."
    ##killall g510s
    echo 'Keyboard unplug event' | systemd-cat
    ;;
  *)
    echo "Usage: $0 {start|stop}" >&2
    exit 1
    ;;
esac

Now open a terminal and switch to root user and run journalctl -f open another terminal and in this terminal run the command /usr/bin/mykeyboard start you should see in the journal output Keyboard plug event? if you change the start to stop and run you should see Keyboard unplug event in the output?

Now, plug in the keyboard, does it show Keyboard plug event again? Likewise if you unplug do you see Keyboard unplug event?

so after reverting to my file with with everything in the same place a chmod-ing the script (which I completely forgot to do,) It will now start the the keyboard manager but kill it immedietly.
here is the code i have

#!/usr/bin/bash
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
case "$1" in
  start)
    echo "Starting G510S..."
    #sudo -u usr_40476 /usr/bin/g510s &
    echo 'Keyboard plug event' | systemd-cat
    ;;
  stop)
    echo "Stopping G510S..."
    killall g510s
    echo 'Keyboard unplug event' | systemd-cat
    ;;
  *)
    echo "Usage: $0 {start|stop}" | systemd-cat
    exit 1
    ;;
esac
# Make uinput load
SUBSYSTEM=="input", RUN+="/sbin/modprobe uinput"

# Set permissions for uinput devices
KERNEL=="event*|uinput", GROUP="plugdev", MODE="0660"

# Logitech G510s and G510
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", MODE="0660", GROUP="plugdev"
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22e", MODE="0660", GROUP="plugdev"

# auto start (does not work)
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", RUN+="/usr/bin/mykeyboard start"
SUBSYSTEM=="usb", ACTION=="remove", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", RUN+="/usr/bin/mykeyboard stop"

@40476 You need to put the rules in a separate file as indicated, you didn’t comment out that kill command…

Correct. RUN in udev is intended for performing quick actions. It is not intended for starting a long running program. That is what SYSTEMD_WANTS is for.

No device unit is generated for the keyboard:

crw------- 1 root root 180, 96 Jul 11 10:37 /dev/usb/hiddev0
erlangen:~ # 

Some tinkering required:

erlangen:~ # cat /etc/udev/rules.d/99-keyboard.rules
SUBSYSTEMS=="usb", ATTRS{bInterfaceProtocol}=="00", TAG+="systemd"
erlangen:~ # 

Unit is activated upon attaching the receiver of the keyboard:

erlangen:~ # systemctl status dev-usb-hiddev0.device
● dev-usb-hiddev0.device - /dev/usb/hiddev0
    Follows: unit currently follows state of sys-devices-pci0000:00-0000:00:02.1-0000:01:00.0-usb1-1\x2d7-1\x2d7:1.2-usbmisc-hiddev0.device
     Loaded: loaded
     Active: active (plugged) since Thu 2024-07-11 10:37:45 CEST; 5min ago
     Device: /sys/devices/pci0000:00/0000:00:02.1/0000:01:00.0/usb1/1-7/1-7:1.2/usbmisc/hiddev0
erlangen:~ # 

Created service:

# /etc/systemd/system/keyboard.service
[Unit]
Description=Notify about keyboard
Requires=dev-usb-hiddev0.device
After=dev-usb-hiddev0.device

[Service]
ExecStart=/usr/bin/echo Attached keyboard

[Install]
WantedBy=dev-usb-hiddev0.device

# /etc/systemd/system/service.d/toplevel-override.conf
[Unit]
OnFailure=failure-notification@%n
erlangen:~ # 

Enabled service is started automatically:

erlangen:~ # journalctl -b -u keyboard.service 
Jul 11 10:37:45 erlangen systemd[1]: Started Notify about keyboard.
Jul 11 10:37:45 erlangen echo[402417]: Attached keyboard
Jul 11 10:37:45 erlangen systemd[1]: keyboard.service: Deactivated successfully.
erlangen:~ # 

so it turns out when the keyboard driver starts it disconnects the device, which kills the driver. So there needs to be a way to prevent that. (I also intend to launch this program from KDE and the terminal.

just did all that.

@40476 So you need to describe what happened?