Search code examples
linuxusblibusbudev

How could be notified once USB device is attached/detached in linux system and read the file from USB


I need to be notified once new USB device is attached/detached in Linux system.

And once new device will be attached I need to read the config file("config/conf.xml") and notify to REST API.

I looked into libusb and udev-rules but not sure what is the best way to achieve this.

Please give your view/suggestion on this.


Solution

  • I'd add udev rule first with running your custom script. Something like:

    ACTION=="add", KERNEL=="sd?1", SUBSYSTEMS=="usb", RUN+="/path/to/your/script %k"
    

    %k is kernel parameter that is passed to your script.

    From udev man:

    $kernel, %k
        The kernel name for this device.
    

    In the script I'd use curl. If you don't need some tricky logic. If so, I'd use python.

    Anyway I think udev is perfect for this problem.