[SOLVED] How to auto-start a program on storage device plug?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to auto-start a program on storage device plug?
I need to auto-mount a storage device AND execute a program passing mount point path to it. How can i do this? I'd like to avoid polling for new storage devices.
I'm looking for something like acpid for ACPI events, but working with storage plug events instead.
Depending on your distro and packages, I believe that you need to learn about udev rules. There are numerous postings here and elsewhere on the web.
Basically,
hardware notices a USB device connection
usb drivers interact with the device and emit what are called events
either hal or device kit get involved with event processing
then udev gets involved with event processing
configured udev rules which are script-like take over to discover the nature of the USB device and do whatever you want to happen.
In all likelihood, your box already does something if you connect a flash drive or insert media into your DVD drive. You want to accomplish those same things but doing your own thing ... but you might know that much already.
The green part matches the device. The blue part is the command line to run. The interesting bit is $devpath.
Here's what the script does with $devpath. It may not be an optimal solution but it works to mount the device. All the error traps and logging have been removed so the essentials stand out
Code:
maj_min="$(cat "/sys$devpath/dev")"
buf="$(ls -l "/dev/block/$maj_min")"
partition_dev_file="/dev/${buf##*/}"
mount $partition_dev_file $mountpoint
Beware udev sets a crazy $PATH so don't rely on it.
For logging, logger -p daemon.info is a good choice but not when you are debugging at the command line when echo is easier!
If you want to display graphical messages from the script, yad is a good choice.
...
Beware udev sets a crazy $PATH so don't rely on it.
...
Can you recommend a good HOWTO about udev and friends? As I read, I get multiple explanations one says, "HAL this and that ..." and another says "HAL is deprecated in favor of DeviceKit ..." When I look at my system, it seems that both are in place.Huh?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.