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?
2 Answers
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.

- 84,103
- 24
- 152
- 189
-
C may not be required depending on what the devices are. – Ignacio Vazquez-Abrams Jul 01 '16 at 20:38
-
Sure, any lanuage that has usable wrappers for libusb and libudev would work. And if your device can be detected in some other way (like with `ls`) you could do that instead. – David Grayson Jul 01 '16 at 21:00
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.

- 27
- 3