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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. Introduction to sed (Stream Editor) : Useful sed Command Examples
  2. How to find and delete files older than some particular time period in Linux
  3. Troubleshooting common NFS issues in Linux
  4. How to extract RAR files in CentOS/RHEL 7 and 8
  5. Oracle Software Group Accounts OSDBA, OSOPER, Oracle Inventory group
  6. How to recover from deleted root entry in /etc/shadow and/or /etc/passwd files in CentOS / RHEL 6
  7. pvcreate error : Can’t open /dev/sdx exclusively. Mounted filesystem?
  8. How to Create and Query a BTRFS File System
  9. The Squid Service in Failed State with Error: “Failed to make swap directory /var/spool/squid/00: (13) Permission denied”
  10. “Couldn’t authenticate with keytab while discovering which salt to use: hostname: KDC has no support for encryption type” – error while joining domain

You May Also Like

Primary Sidebar

Recent Posts

  • MySQL: how to figure out which session holds which table level or global read locks
  • Recommended Configuration of the MySQL Performance Schema
  • MySQL: Identify what user and thread are holding on to a meta data lock that is preventing other queries from running
  • MySQL: How to kill a Long Running Query using max_execution_time
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary