简体   繁体   English

在Linux系统中连接/分离USB设备并从USB读取文件后如何通知

[英]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. 在Linux系统中连接/分离新的USB设备后,需要通知我。

And once new device will be attached I need to read the config file("config/conf.xml") and notify to REST API. 一旦连接了新设备,我需要读取配置文件(“ config / conf.xml”)并通知REST API。

I looked into libusb and udev-rules but not sure what is the best way to achieve this. 我研究了libusbudev-rules,但不确定实现此目标的最佳方法是什么。

Please give your view/suggestion on this. 请对此发表您的看法/建议。

I'd add udev rule first with running your custom script. 我先运行您的自定义脚本来添加udev规则。 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. %k是传递给脚本的内核参数。

From udev man: 来自udev人:

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

In the script I'd use curl . 在脚本中,我将使用curl If you don't need some tricky logic. 如果您不需要一些棘手的逻辑。 If so, I'd use python. 如果是这样,我会使用python。

Anyway I think udev is perfect for this problem. 无论如何,我认为udev非常适合该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM