• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Run a Script When USB Devices Is Attached or Removed Using UDEV

by admin

The post outlines steps to create UDEV rules that run a specific script when a USB device is attached or removed. We can use the udevadm tool to see the information and properties when a device is attached or removed. Using the “subsystem-match=SUBSYSTEM” We can determinate the kind of device that is attached/removed.

1. For example, in this case with a USB SUBSYSTEM, you can run command below:

# udevadm monitor --kernel --property --subsystem-match=usb
monitor will print the received events for:
KERNEL - the kernel uevent

2. Now, attach or remove the USB device, to trigger a UDEV event.

3. Below is the example of output, the information with “<---" lines is important information to considerate to set the rules.

# udevadm monitor --kernel --property --subsystem-match=usb
monitor will print the received events for:
KERNEL - the kernel uevent
KERNEL[6147.486744] add /devices/pci0000:00/0000:00:0c.0/usb1/1-2 (usb)
ACTION=add             <--------------------------------------------------------- ACTION of the device
BUSNUM=001
DEVNAME=/dev/bus/usb/001/010
DEVNUM=010
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2
DEVTYPE=usb_device      
MAJOR=189
MINOR=9
PRODUCT=90c/1000/1100  <--------------------------------------------------------- ENV{PRODUCT}
SEQNUM=2192
SUBSYSTEM=usb          <--------------------------------------------------------- " SUBSYSTEM=="type_of_device"
TYPE=0/0/0

KERNEL[6147.489141] add /devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0
DEVTYPE=usb_interface
INTERFACE=8/6/80
MODALIAS=usb:v090Cp1000d1100dc00dsc00dp00ic08isc06ip50in00
PRODUCT=90c/1000/1100
SEQNUM=2193
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[6206.530205] remove /devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0 (usb)
ACTION=remove           <--------------------------------------------------------- ACTION of the device 
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2/1-2:1.0
DEVTYPE=usb_interface
INTERFACE=8/6/80
MODALIAS=usb:v090Cp1000d1100dc00dsc00dp00ic08isc06ip50in00
PRODUCT=90c/1000/1100   <--------------------------------------------------------- ENV{PRODUCT}
SEQNUM=2216
SUBSYSTEM=usb           <--------------------------------------------------------- " SUBSYSTEM=="type_of_device"
TYPE=0/0/0
KERNEL[6206.530551] remove /devices/pci0000:00/0000:00:0c.0/usb1/1-2 (usb)
ACTION=remove
BUSNUM=001
DEVNAME=/dev/bus/usb/001/010
DEVNUM=010
DEVPATH=/devices/pci0000:00/0000:00:0c.0/usb1/1-2
DEVTYPE=usb_device
MAJOR=189
MINOR=9
PRODUCT=90c/1000/1100
SEQNUM=2217
SUBSYSTEM=usb
TYPE=0/0/0

4. Using above example, you can create the rules to run scripts in /etc/udev/rules.d/. For example, you can create a new file "5-usbstick-font.rules", with a syntax similar to :

# cat /etc/udev/rules.d/5-usbstick-font.rules
ACTION=="add", SUBSYSTEM=="usb", ENV{PRODUCT}=="90c/1000/1100", RUN=="/bin/su godiego --command='/home/godiego/run-start.sh'"
ACTION=="remove", SUBSYSTEM=="usb", ENV{PRODUCT}=="90c/1000/1100", RUN+="/bin/su godiego --command='/home/godiego/run-stop.sh'"

5. Reload the UDEV rule by running the below command.

# udevadm control --reload

Now, if you attach or remove the USB device again, the customized script (like run-start.sh or run-stop.sh) will be executed.

Filed Under: CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. “mdadm: No arrays found in config file” – error on running ‘mdadm –assemble –scan’
  2. vmstat: command not found
  3. who: command not found
  4. CentOS / RHEL : How to add Physical Volume (PV) to a Volume group (VG) in LVM
  5. hardinfo: command not found
  6. How to Delete unnecessary Entry in /etc/shadow
  7. How to delete unused kernels and keep only the running kernel in CentOS/RHEL
  8. CentOS / RHEL 7 : Tips on Troubleshooting NTP / chrony Issues
  9. squeue Command Examples in Linux
  10. Linux OS Service ‘rpcidmapd’

You May Also Like

Primary Sidebar

Recent Posts

  • Chezmoi: A multi-machine dotfile manager, written in Go
  • cheat: Create and view interactive cheat sheets on the command-line
  • chars: Display names and codes for various ASCII and Unicode characters and code points
  • chafa: Image printing in the terminal

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright