LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to auto-start a program on storage device plug? (https://www.linuxquestions.org/questions/linux-software-2/how-to-auto-start-a-program-on-storage-device-plug-806554/)

10110111 05-07-2010 03:46 PM

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.

SaintDanBert 05-07-2010 04:05 PM

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.

Cheers,
~~~ 0;-Dan

10110111 05-08-2010 05:50 AM

Thank you. I knew there has to be something, just forgot about udev.

catkin 05-08-2010 06:15 AM

Here's a udev rule that calls a script
Code:

SUBSYSTEM=="block", ATTR{size}=="976768002", ATTRS{product}=="SimpleDrive mini", ATTRS{serial}=="2512009121800442", ACTION=="add", RUN+="/lib/udev/local/usb_hdd_for_bacula_sync.sh -d add $devpath"
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.

SaintDanBert 05-08-2010 09:33 AM

Quote:

Originally Posted by catkin (Post 3961238)
...
If you want to display graphical messages from the script, yad is a good choice.
...

I've looked for yad without result. Is this part of some larger package?

What I really want to do is hook in with the built-in notifier feature of my Ubuntu Jaunty. Can someone tell me how to do that?

~~~ 0;-Dan

SaintDanBert 05-08-2010 09:38 AM

Quote:

Originally Posted by catkin (Post 3961238)
Here's a udev rule that calls a script
Code:

SUBSYSTEM=="block", ATTR{size}=="976768002", ATTRS{product}=="SimpleDrive mini", ATTRS{serial}=="2512009121800442", ACTION=="add", RUN+="/lib/udev/local/usb_hdd_for_bacula_sync.sh -d add $devpath"
...
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?

~~~ 0;-Dan

10110111 05-08-2010 11:17 AM

Quote:

When I look at my system, it seems that both are in place.Huh?
Yes, not all the programs have switched from HAL to DeviceKit, e.g. KDE, so you have to have both HAL&DeviceKit on Kubuntu :)
Quote:

What I really want to do is hook in with the built-in notifier feature of my Ubuntu Jaunty. Can someone tell me how to do that?
Look at eeepc-acpi-scripts. They have notification scripts based on pynotify.


All times are GMT -5. The time now is 12:32 AM.