I have a C++ program that accesses USB pen drives/flash drives. It works for currently inserted flash drive. A normal C++ program doesn't execute until we run it. But I wanted the program to run automatically whenever a flash drive is inserted. How can I do that?
USB – Execute C++ Program When USB Flash Drive is Inserted
cudevusbusb-drive
Related Question
- Ubuntu – How to execute a file from a FAT USB drive
- Ubuntu – What happens under the hood when an USB drive is inserted
- Ubuntu – How to temporarily suspend Nautilus filesystem automounting, without making permanent changes to the system and user configuration
- Ubuntu Doesn’t Automatically Mount USB Flash Drive – Solution
Best Answer
For general use, If you would like to run your program for any USB storage. Use the driver for the rule match.
Add a
udev
rules fileAdd this rule
If you want your program to distinguish the disks, so it runs different operations, use (you can pass its serial number or any attribute you like):
Reload all rules
Unplug and replug the flash drive
Notes:
I used this rule just to test which create a log when the rule is triggered:
You can comment the rules you don't want by adding
#
to the beginning of the line. Rules file can contain multiple rules.To check all the available
env
variables, use:To check for parameters to use for rule match, run:
References: