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

I have a keyboard driver, and I want it to launch as the user I am logged in as when ever the device 046d:c22d is connected, and when it is disconnected I want the program to be killed. I thought maybe this was possible with udev but was not sure if it would run as root or only run when I was logged in.

@40476 what is the device 046d:c22d ?

Bus 001 Device 050: ID 046d:c22d Logitech, Inc. G510 Gaming Keyboard

@40476 and what program for the keyboard?

the program is “g510s” (the one you helped me with)

@40476 Have a look at this then https://github.com/vividnightmare/g510s/blob/master/99-g510s.rules I would suggest adapting to run the bin file as your user after copying to /etc/udev/rules.d. The issue will be stopping on unplug…

but would it run as my user when i was logged in?

I dont see a way to run it as my current user when logged in though.

@40476 So if you run (as root user) udevadm monitor and capture the input from plugging in the device and the unplugging the device.

Use that to trigger a udev event starting a user systemd service then one to stop…

@40476 test the following to see which works best for you…

#!/usr/bin/bash

case "$1" in
  start)
    echo "Starting G510S daemon..."
    /usr/bin/g510s &
    ;;
  stop)
    echo "Stopping G510S daemon..."
    killall g510s
    ;;
  *)
    echo "Usage: $0 {start|stop}" >&2
    exit 1
    ;;
esac
##$HOME/.config/systemd/user/g510s.service

[Unit]
Description=G510S

[Service]
ExecStart=/usr/bin/g510s
Restart=always

[Install]
WantedBy=graphical.target

plug in:

usr_40476@localhost:~> sleep 5 && udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[259421.781191] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
KERNEL[259421.790482] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
KERNEL[259421.790797] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
KERNEL[259421.794348] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
KERNEL[259421.795009] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/wakeup/wakeup45 (wakeup)
KERNEL[259421.795684] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96 (input)
KERNEL[259421.851053] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
KERNEL[259421.851223] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
KERNEL[259421.851324] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
KERNEL[259421.851443] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
KERNEL[259421.851541] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
KERNEL[259421.851656] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
KERNEL[259421.851755] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::compose (leds)
KERNEL[259421.851852] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
KERNEL[259421.851967] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
KERNEL[259421.852109] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/event4 (input)
KERNEL[259421.852258] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/hidraw/hidraw1 (hidraw)
KERNEL[259421.852444] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
KERNEL[259421.852613] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
KERNEL[259421.852771] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
KERNEL[259421.853614] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
KERNEL[259421.854190] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98 (input)
KERNEL[259421.911256] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98/event5 (input)
KERNEL[259421.911467] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/hidraw/hidraw2 (hidraw)
KERNEL[259421.915864] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97 (input)
KERNEL[259421.916140] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97/event6 (input)
KERNEL[259421.916320] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::kbd_backlight (leds)
KERNEL[259421.916465] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::power_on_backlight_val (leds)
KERNEL[259421.916602] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset1 (leds)
KERNEL[259421.917249] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset2 (leds)
KERNEL[259421.917518] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset3 (leds)
KERNEL[259421.918427] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_record (leds)
KERNEL[259421.919310] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
KERNEL[259421.919506] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
KERNEL[259421.919752] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
UDEV  [259422.557047] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
UDEV  [259422.587756] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
UDEV  [259422.600094] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
UDEV  [259422.605800] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
UDEV  [259422.606533] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/wakeup/wakeup45 (wakeup)
UDEV  [259422.608283] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
UDEV  [259422.614400] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
UDEV  [259422.637705] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::power_on_backlight_val (leds)
UDEV  [259422.637805] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset1 (leds)
UDEV  [259422.660632] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset3 (leds)
UDEV  [259422.660921] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96 (input)
UDEV  [259422.661097] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::kbd_backlight (leds)
UDEV  [259422.661229] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_record (leds)
UDEV  [259422.661386] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
UDEV  [259422.661583] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97 (input)
UDEV  [259422.668399] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
UDEV  [259422.668508] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98 (input)
UDEV  [259422.671260] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset2 (leds)
UDEV  [259422.673136] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::compose (leds)
UDEV  [259422.673227] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
UDEV  [259422.674273] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
UDEV  [259422.677993] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/hidraw/hidraw1 (hidraw)
UDEV  [259422.678222] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/hidraw/hidraw2 (hidraw)
UDEV  [259422.678491] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
UDEV  [259422.679050] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
UDEV  [259422.679796] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
UDEV  [259422.682338] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
UDEV  [259422.720876] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/event4 (input)
UDEV  [259422.730484] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
UDEV  [259422.732684] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
UDEV  [259422.747293] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97/event6 (input)
UDEV  [259422.753313] add      /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98/event5 (input)
UDEV  [259422.753376] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
UDEV  [259422.754558] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
UDEV  [259422.766030] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
^C
usr_40476@localhost:~> 

unplug :

usr_40476@localhost:~udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[259671.488056] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
KERNEL[259671.488272] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
KERNEL[259671.514212] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
KERNEL[259671.514276] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
KERNEL[259671.537581] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
KERNEL[259671.537655] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
KERNEL[259671.537692] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::compose (leds)
UDEV  [259671.542448] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::compose (leds)
KERNEL[259671.554314] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
KERNEL[259671.554397] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
KERNEL[259671.581340] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/event4 (input)
KERNEL[259671.600018] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96 (input)
KERNEL[259671.602316] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/hidraw/hidraw1 (hidraw)
KERNEL[259671.602411] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
KERNEL[259671.602465] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
KERNEL[259671.602525] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
KERNEL[259671.602579] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
UDEV  [259671.609837] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/hidraw/hidraw1 (hidraw)
UDEV  [259671.611771] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/event4 (input)
KERNEL[259671.661031] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98/event5 (input)
UDEV  [259671.677207] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98/event5 (input)
KERNEL[259671.678384] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98 (input)
KERNEL[259671.678432] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/hidraw/hidraw2 (hidraw)
KERNEL[259671.678462] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_record (leds)
KERNEL[259671.678483] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset3 (leds)
UDEV  [259671.681454] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/hidraw/hidraw2 (hidraw)
KERNEL[259671.685415] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset2 (leds)
UDEV  [259671.687642] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset2 (leds)
UDEV  [259671.688097] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset3 (leds)
UDEV  [259671.688491] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_record (leds)
KERNEL[259671.688538] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset1 (leds)
KERNEL[259671.688571] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::power_on_backlight_val (leds)
KERNEL[259671.688600] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::kbd_backlight (leds)
UDEV  [259671.691763] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::power_on_backlight_val (leds)
UDEV  [259671.691822] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::kbd_backlight (leds)
UDEV  [259671.691862] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/leds/g15::macro_preset1 (leds)
UDEV  [259671.691953] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input98 (input)
KERNEL[259671.707622] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97/event6 (input)
UDEV  [259671.716814] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97/event6 (input)
KERNEL[259671.727572] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97 (input)
KERNEL[259671.728383] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
KERNEL[259671.728505] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
KERNEL[259671.728556] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
KERNEL[259671.728614] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
UDEV  [259671.736827] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
KERNEL[259671.736897] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
UDEV  [259671.736958] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
KERNEL[259671.736989] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/wakeup/wakeup45 (wakeup)
UDEV  [259671.737040] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
KERNEL[259671.737083] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
UDEV  [259671.737123] change   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
UDEV  [259671.737361] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026/input/input97 (input)
UDEV  [259671.738807] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::kana (leds)
UDEV  [259671.738887] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::capslock (leds)
UDEV  [259671.740048] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::numlock (leds)
UDEV  [259671.740485] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96/input96::scrolllock (leds)
UDEV  [259671.749007] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025/input/input96 (input)
UDEV  [259671.868759] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
UDEV  [259671.868878] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
UDEV  [259671.870338] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0/0003:046D:C22D.0025 (hid)
UDEV  [259671.870454] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1/0003:046D:C22D.0026 (hid)
UDEV  [259671.872653] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
UDEV  [259671.874469] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
UDEV  [259671.875077] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.1 (usb)
UDEV  [259671.878513] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/1-9.6:1.0 (usb)
UDEV  [259671.886484] unbind   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)
UDEV  [259671.891357] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6/wakeup/wakeup45 (wakeup)
UDEV  [259671.932869] remove   /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.6 (usb)

@40476 so if you add the following udev rule as 99-mykeyboard.rules

SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", TAG+="/usr/bin/mykeyboard start"
SUBSYSTEM=="usb", ACTION=="remove", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", TAG+="/usr/bin/mykeyboard stop"

Add that start/stop script to a file called /usr/bin/mykeyboard and chmod 0755 the script.

Reload the udev rules with udevadm control --reload-rules and test.

would this work? or would it run as root?

# Make uinput load
SUBSYSTEM=="input", RUN+="/sbin/modprobe uinput"

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

# Logitech G510 and G510s
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
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", TAG+="/usr/bin/g510s"
SUBSYSTEM=="usb", ACTION=="remove", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", TAG+="/usr/bin/killall g510s"

Try it and see what happens

nothing happens.

should a try it as a systemd service?

@40476 OK, so another test… Add these two rules and remove the old one;

/etc/udev/rules.d/90-keyboard-add.rules 
SUBSYSTEM=="usb", DRIVER=="usb", ACTION=="add", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", RUN+="/usr/bin/mykeyboard start"

/etc/udev/rules.d/91-keyboard-remove.rules 
ACTION=="remove", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c22d", RUN+="/usr/bin/keyboard_event /usr/bin/mykeyboard stop"

Once those two are added run udevadm control --reload and test plugging in the device and unplugging. The rules are slightly different as only want to trigger on one udev event…

Tinkering with udev is the hard way, in my experience of course. I prefer the easy way. Upon connecting the Forerunner® 735XT it gets mounted through /etc/fstab:

erlangen:~ # grep /FR735 /etc/fstab 
LABEL=FR735                                /FR735                  vfat   user,x-systemd.automount,x-systemd.idle-timeout=10 0  0
erlangen:~ # 

The following service is wanted by the mount:

erlangen:~ # systemctl cat FR735.service 
# /etc/systemd/system/FR735.service
[Unit]
Description=Get FR735 Activities
Requires=FR735.mount
After=FR735.mount

[Service]
ExecStart=/usr/bin/rsync -av /FR735/ /home/karl/Forerunner/

[Install]
WantedBy=FR735.mount

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

While charging the battery of the Forerunner new training units are copied automatically:

erlangen:~ # journalctl --since yesterday -u FR735.service 
Jul 09 19:34:18 erlangen systemd[1]: Started Get FR735 Activities.
Jul 09 19:34:18 erlangen rsync[228543]: sending incremental file list
Jul 09 19:34:19 erlangen rsync[228543]: AUTORUN.INF
Jul 09 19:34:19 erlangen rsync[228543]: GARMIN/DEVICE.FIT
Jul 09 19:34:19 erlangen rsync[228543]: GARMIN/GarminDevice.xml
Jul 09 19:34:19 erlangen rsync[228543]: GARMIN/ACTIVITY/E79E5706.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/APPS/OUT.BIN
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/EVNTLOGS/0000055B.TXT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/LOCATION/LCTNS.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/MLTSPORT/
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/MLTSPORT/0TRIATH.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/MONITOR/E79J3300.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/NEWFILES/
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/RECORDS/RECORDS.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SCHEDULE/SCHEDULE.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SETTINGS/SETTINGS.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SPORTS/
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SPORTS/0RLAUFE.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SPORTS/1RINDOO.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SPORTS/2CRADFA.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SPORTS/3CINDOO.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SPORTS/4SFREIW.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/SPORTS/5SSCHWI.FIT
Jul 09 19:34:20 erlangen rsync[228543]: GARMIN/TOTALS/TOTALS.FIT
Jul 09 19:34:20 erlangen rsync[228543]: sent 162,331 bytes  received 443 bytes  65,109.60 bytes/sec
Jul 09 19:34:20 erlangen rsync[228543]: total size is 7,081,815  speedup is 43.51
Jul 09 19:34:20 erlangen systemd[1]: FR735.service: Deactivated successfully.
erlangen:~ # 

@karlmistelberger unfortunately this is not a Garmin device it’s a Logitech Keyboard with a LCD display… :wink:

The procedure works with mount units. Devices come with their own units, e.g.:

erlangen:~ # systemctl list-units sys-subsystem-bluetooth-devices-hci0.device
  UNIT                                        LOAD   ACTIVE SUB     DESCRIPTION                          
  sys-subsystem-bluetooth-devices-hci0.device loaded active plugged /sys/subsystem/bluetooth/devices/hci0

Legend: LOAD   → Reflects whether the unit definition was properly loaded.
        ACTIVE → The high-level unit activation state, i.e. generalization of SUB.
        SUB    → The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
erlangen:~ # 

The service unit will refer to the appropriate device unit instead of the mount unit in the above example service.