-1

There are perhaps 1000 or more web resources that explain how one can run an application when a USB device is connected. What I'm looking for is a technique that qualifies an action on two specific devices being present (they can be connected in any order and at any time). I've thought about a scheme where each device has its own action that creates a distinct transient file, then checks to see if the other device has created its file. This seems cumbersome and may be prone to races. Is there a better way to approach the problem?

5nh
  • 27
  • 3

2 Answers2

1

First, figure out how to trigger a program when USB devices are connected using those resources you mentioned. Then write a program in C that uses libusb or libudev to check for the presence of both devices. If both devices are found, trigger whatever action you want to happen.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

Invoking long-running processes from udev rules is quite difficult in newer Linux distributions, but leaving the particulars of that battle aside I took Davids advice. Both udev rules invoke the same Python script, which checks for both devices and starts the application if they are present. It took a little bit of work to prevent races between two concurrent instances of the script, but I ended using rename of a file under /var/run/myapp as a mutex. Still need a 'remove' rule to kill the process, but that's the easier piece.

5nh
  • 27
  • 3