This is a duplicate of the question "Is there a way to automatically activate a script when a USB device connects?" from 2014 but the solution presented does not work for me.
I would like to run a script when any device (drive, keyboard, ...) connects to any USB port. Following the answer to the question above, I tried to build a rule for that 5I commented out various other incantations of the rules):
# cat /etc/udev/rules.d/plug_usb.rules
ACTION=="add", RUN+="/bin/date > /tmp/date.txt"
#ACTION=="add", DEVTYPE="usb_device", RUN+="/bin/date > /tmp/date.txt"
#KERNEL="*", RUN+="/bin/date > /tmp/date.txt"
#SUBSYSTEMS=="usb", RUN+="/bin/date > /tmp/date.txt"
This is the simplest rule I can imagine (when and "add" action is trigerred, run the script). I would like to constraint it to USB devices but at this point I wanted to start from the most basic one.
I restarted udev via systemctl restart udev
and udevadm control --reload-rules
and plugged in a USB drive. The script was not ran (no /tmp/date.txt
file).
When inserting the USB drive I do see lots of events:
# udevadm monitor -p -k -u
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
KERNEL[1775.965890] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4 (usb)
ACTION=add
BUSNUM=001
DEVNAME=/dev/bus/usb/001/019
DEVNUM=019
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4
DEVTYPE=usb_device
MAJOR=189
MINOR=18
PRODUCT=90c/1000/1000
SEQNUM=1947
SUBSYSTEM=usb
TYPE=0/0/0
KERNEL[1775.967288] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0 (usb)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0
DEVTYPE=usb_interface
INTERFACE=8/6/80
MODALIAS=usb:v090Cp1000d1000dc00dsc00dp00ic08isc06ip50in00
PRODUCT=90c/1000/1000
SEQNUM=1948
SUBSYSTEM=usb
TYPE=0/0/0
KERNEL[1775.968292] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15 (scsi)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15
DEVTYPE=scsi_host
SEQNUM=1949
SUBSYSTEM=scsi
KERNEL[1775.968577] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/scsi_host/host15 (scsi_host)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/scsi_host/host15
SEQNUM=1950
SUBSYSTEM=scsi_host
UDEV [1775.979656] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4 (usb)
ACTION=add
BUSNUM=001
DEVNAME=/dev/bus/usb/001/019
DEVNUM=019
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4
DEVTYPE=usb_device
ID_BUS=usb
ID_MODEL=USB_DISK
ID_MODEL_ENC=USB\x20DISK
ID_MODEL_FROM_DATABASE=Flash Drive
ID_MODEL_ID=1000
ID_REVISION=1000
ID_SERIAL=090c_USB_DISK_15111933000107
ID_SERIAL_SHORT=15111933000107
ID_USB_INTERFACES=:080650:
ID_VENDOR=090c
ID_VENDOR_ENC=090c
ID_VENDOR_FROM_DATABASE=Silicon Motion, Inc. - Taiwan (formerly Feiya Technology Corp.)
ID_VENDOR_ID=090c
MAJOR=189
MINOR=18
PRODUCT=90c/1000/1000
SEQNUM=1947
SUBSYSTEM=usb
TYPE=0/0/0
USEC_INITIALIZED=75965869
UDEV [1775.987989] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0 (usb)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0
DEVTYPE=usb_interface
ID_MODEL_FROM_DATABASE=Flash Drive
ID_VENDOR_FROM_DATABASE=Silicon Motion, Inc. - Taiwan (formerly Feiya Technology Corp.)
INTERFACE=8/6/80
MODALIAS=usb:v090Cp1000d1000dc00dsc00dp00ic08isc06ip50in00
PRODUCT=90c/1000/1000
SEQNUM=1948
SUBSYSTEM=usb
TYPE=0/0/0
USEC_INITIALIZED=967657
UDEV [1775.994239] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15 (scsi)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15
DEVTYPE=scsi_host
SEQNUM=1949
SUBSYSTEM=scsi
USEC_INITIALIZED=969049
UDEV [1776.001861] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/scsi_host/host15 (scsi_host)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/scsi_host/host15
SEQNUM=1950
SUBSYSTEM=scsi_host
USEC_INITIALIZED=969180
KERNEL[1777.370293] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0 (scsi)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0
DEVTYPE=scsi_target
SEQNUM=1951
SUBSYSTEM=scsi
KERNEL[1777.370439] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0 (scsi)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0
DEVTYPE=scsi_device
MODALIAS=scsi:t-0x00
SEQNUM=1952
SUBSYSTEM=scsi
KERNEL[1777.370584] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_disk/15:0:0:0 (scsi_disk)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_disk/15:0:0:0
SEQNUM=1953
SUBSYSTEM=scsi_disk
KERNEL[1777.370662] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_device/15:0:0:0 (scsi_device)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_device/15:0:0:0
SEQNUM=1954
SUBSYSTEM=scsi_device
KERNEL[1777.371165] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_generic/sg0 (scsi_generic)
ACTION=add
DEVNAME=/dev/sg0
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_generic/sg0
MAJOR=21
MINOR=0
SEQNUM=1955
SUBSYSTEM=scsi_generic
KERNEL[1777.371799] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/bsg/15:0:0:0 (bsg)
ACTION=add
DEVNAME=/dev/bsg/15:0:0:0
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/bsg/15:0:0:0
MAJOR=252
MINOR=0
SEQNUM=1956
SUBSYSTEM=bsg
KERNEL[1777.374198] add /devices/virtual/bdi/8:0 (bdi)
ACTION=add
DEVPATH=/devices/virtual/bdi/8:0
SEQNUM=1957
SUBSYSTEM=bdi
UDEV [1777.374832] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0 (scsi)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0
DEVTYPE=scsi_target
SEQNUM=1951
SUBSYSTEM=scsi
USEC_INITIALIZED=370884
UDEV [1777.376907] add /devices/virtual/bdi/8:0 (bdi)
ACTION=add
DEVPATH=/devices/virtual/bdi/8:0
SEQNUM=1957
SUBSYSTEM=bdi
USEC_INITIALIZED=77375231
UDEV [1777.383332] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0 (scsi)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0
DEVTYPE=scsi_device
MODALIAS=scsi:t-0x00
SEQNUM=1952
SUBSYSTEM=scsi
USEC_INITIALIZED=371005
KERNEL[1777.385276] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/block/sda (block)
ACTION=add
DEVNAME=/dev/sda
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/block/sda
DEVTYPE=disk
MAJOR=8
MINOR=0
SEQNUM=1958
SUBSYSTEM=block
UDEV [1777.387755] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/bsg/15:0:0:0 (bsg)
ACTION=add
DEVNAME=/dev/bsg/15:0:0:0
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/bsg/15:0:0:0
MAJOR=252
MINOR=0
SEQNUM=1956
SUBSYSTEM=bsg
USEC_INITIALIZED=77375131
UDEV [1777.391037] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_device/15:0:0:0 (scsi_device)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_device/15:0:0:0
SEQNUM=1954
SUBSYSTEM=scsi_device
USEC_INITIALIZED=371101
UDEV [1777.392879] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_disk/15:0:0:0 (scsi_disk)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_disk/15:0:0:0
SEQNUM=1953
SUBSYSTEM=scsi_disk
USEC_INITIALIZED=371059
UDEV [1777.397161] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_generic/sg0 (scsi_generic)
ACTION=add
DEVNAME=/dev/sg0
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/scsi_generic/sg0
MAJOR=21
MINOR=0
SEQNUM=1955
SUBSYSTEM=scsi_generic
USEC_INITIALIZED=77374811
UDEV [1777.436968] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/block/sda (block)
ACTION=add
DEVLINKS=/dev/disk/by-id/usb-090c_USB_DISK_15111933000107-0:0 /dev/disk/by-label/ESD_ISO /dev/disk/by-path/platform-3f980000.usb-usb-0:1.4:1.0-scsi-0:0:0:0 /dev/disk/by-uuid/2016-07-21-13-55-06-00
DEVNAME=/dev/sda
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host15/target15:0:0/15:0:0:0/block/sda
DEVTYPE=disk
ID_BUS=usb
ID_FS_BOOT_SYSTEM_ID=EL\x20TORITO\x20SPECIFICATION
ID_FS_LABEL=ESD_ISO
ID_FS_LABEL_ENC=ESD_ISO
ID_FS_TYPE=udf
ID_FS_USAGE=filesystem
ID_FS_UUID=2016-07-21-13-55-06-00
ID_FS_UUID_ENC=2016-07-21-13-55-06-00
ID_INSTANCE=0:0
ID_MODEL=USB_DISK
ID_MODEL_ENC=USB\x20DISK
ID_MODEL_ID=1000
ID_PATH=platform-3f980000.usb-usb-0:1.4:1.0-scsi-0:0:0:0
ID_PATH_TAG=platform-3f980000_usb-usb-0_1_4_1_0-scsi-0_0_0_0
ID_REVISION=1000
ID_SERIAL=090c_USB_DISK_15111933000107-0:0
ID_SERIAL_SHORT=15111933000107
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=090c
ID_VENDOR_ENC=090c
ID_VENDOR_ID=090c
MAJOR=8
MINOR=0
SEQNUM=1958
SUBSYSTEM=block
TAGS=:systemd:
USEC_INITIALIZED=77386166
I expected that using the add
action combined with DEVTYPE=usb_device
would be ideal ("add a USB device"). I also searched on Debian and Ubuntu forums buit the solutions all gravitate around the same idea - so I must be doing something really wrong.
What would be the suitable rule for such an event?