Start systemd .service from an udev rule
udev monitor
allows you to monitor kernel uevents and print the devpaths to the console, which can then be used by udevadm info -a --path=''
to print all sysfs properties for all devices along the chain.
udevadm monitor --environment --udev
lets you monitor environment variable set by udev events after the rule processing.
$ cat /etc/udev/rules.d/99-usb-autorun.rules
# Start USB autorun systemd .service
ENV{DEVTYPE}=="usb_device", \
TAG+="systemd", ENV{SYSTEMD_WANTS}="usb_autorun.service"
(dont forget to reload your rule udevadm control --reload && udevadm trigger
).
...and /etc/systemd/system/usb_autorun.service:
[Unit]
Description=USB Autorun.
[Service]
Type=oneshot
ExecStart=/opt/bin/usb_autorun.sh