Posted on
Questions and Answers

Detect USB device insertion/removal via `udevadm monitor` in a script

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Monitoring USB Devices in Linux Using udevadm monitor

When working with Linux, understanding how your system interacts with connected devices—particularly USB devices—can be crucial for both system administrators and enthusiasts. One powerful tool in the Linux toolkit is udevadm, which can be used to monitor and manage device events. Let's dive into how you can use udevadm monitor to detect USB devices as they are inserted or removed from your system.

What is udevadm?

Q: What does udevadm do in Linux?

A: udevadm stands for "udev administration" and is a utility to manage and debug the udev device manager daemon's actions. Udev dynamically manages device nodes in the /dev directory, handles all user space events related to device addition/removal and executes respective actions to configure devices when they appear/disappear from the system.

How to Detect USB Device Changes with udevadm monitor

Q: How can I use udevadm to monitor USB device changes?

A: Using udevadm monitor, you can watch real-time events that udev generates when devices are inserted or removed. This is particularly useful for USB devices. The command udevadm monitor --kernel --subsystem-match=usb allows you to filter and display USB-related events directly from the kernel.

Some Simple Examples of udevadm monitor

Monitoring all device events:

udevadm monitor

Filtering to only observe events related to USB devices:

udevadm monitor --subsystem-match=usb

Filtering for kernel and USB subsystem events:

udevadm monitor --kernel --subsystem-match=usb

Implementing a Bash Script to Alert on USB Changes

To demonstrate the power of using udevadm in a practical script, we can create a simple Bash script that alerts the user each time a USB device is inserted or removed.

Bash Script for USB Device Detection

#!/bin/bash

# Monitor USB device events
udevadm monitor --kernel --subsystem-match=usb |
while read -r line; do
    if [[ "$line" == *"add@"* ]]; then
        echo "USB device inserted"
    elif [[ "$line" == *"remove@"* ]]; then
        echo "USB device removed"
    fi
done

Explanation: 1. The script starts monitoring USB device events from the kernel. 2. It reads each line of output from udevadm. 3. It checks if the line contains "add@" or "remove@" to determine whether a device was added or removed. 4. It then prints a message to the console accordingly.

Summary and Conclusion

Using udevadm to monitor USB device changes offers powerful capabilities for managing how devices interact with your Linux system. Whether you're developing a complex hardware integration project or simply want an alert system for USB devices, udevadm provides essential, real-time insights into device management actions. The Bash script provided is a fundamental example, but it can be expanded with more complex logic and interactions tailored to specific requirements. Leveraging udevadm, system administrators and developers can automate responses to external device events, enhancing system functionality and reliability.

Understanding and using tools like udevadm not only simplifies device management but also opens up new possibilities for creative and efficient system administration.

Further Reading

Further reading on udevadm and related topics can be found at the following links:

  • Understanding udev on Linux: A general overview of udev, its architecture, and principles. Linux udev overview

  • Advanced Bash Scripting for Device Monitoring: Dive deeper into scripting with Bash for more complex device monitoring tasks. Advanced Bash Scripting

  • Using udev for Device Management: Detailed examples and scenarios where udev can be effective for device management. Using udev in Linux

  • Debugging udev Rules: Learn how to debug udev rules to effectively manage device nodes. Debugging udev rules

  • udevadm Monitor Tutorial: A comprehensive tutorial on using udevadm monitor specifically tailored for USB devices. udevadm Monitor Guide