LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 05-07-2010, 03:46 PM   #1
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Rep: Reputation: 51
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.
 
Old 05-07-2010, 04:05 PM   #2
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
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
 
Old 05-08-2010, 05:50 AM   #3
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Original Poster
Rep: Reputation: 51
Thank you. I knew there has to be something, just forgot about udev.
 
Old 05-08-2010, 06:15 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 05-08-2010, 09:33 AM   #5
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by catkin View Post
...
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
 
Old 05-08-2010, 09:38 AM   #6
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by catkin View Post
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
 
Old 05-08-2010, 11:17 AM   #7
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Original Poster
Rep: Reputation: 51
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to release /dev entry of unplugged storage device while a program has a file open 10110111 Linux - Software 4 11-09-2009 02:21 PM
help with auto run script when usb storage device is plugged in twon Linux - General 3 06-30-2009 03:48 PM
auto start a program in XFree on Fedora? MCSquared Fedora 22 09-12-2007 05:55 PM
Auto start a program in GNOME 2.6 rusty0412 Linux - Newbie 1 05-23-2004 02:47 AM
auto start a program jrockey Linux - Newbie 1 05-26-2001 07:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration