LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   How to make a script run at every mount of pendrives (https://www.linuxquestions.org/questions/slackware-14/how-to-make-a-script-run-at-every-mount-of-pendrives-758688/)

indiajoe 09-30-2009 09:46 AM

How to make a script run at every mount of pendrives
 
Hi,
I wanted to run a script everytime a pendrive (or any external storage device) gets mounted. In my slax-atma distro (a derivative from slax) , the pendrives are automatically mounted using HAL.
Is there any way I can make my script run each time a pendrive gets mounted?
Thanking you in advance,
- Joe

sploot 09-30-2009 02:48 PM

Assuming you have udev:

Code:

File: etc/udev/rules.d/90-hal.rules
 SUBSYSTEM=="block", ACTION=="remove", RUN+="/your/script/goes/here"

Translation: Everytime a block device is removed (This should only be usb disks) run the script entered.

After which, restart HAL.

indiajoe 10-01-2009 10:42 AM

Thanks a lot, just one more doubt..
 
Hi,
Thanks a lot. I never realised such a vast possibility exists in udev. Now I started studying more about udev. One thing I couldn't figure out is how I can run commands like /usr/bin/kdialog from scripts run from udev. ( I am facing the same problem from crontab). Why is it not working even after I redirect it into >>/dev/pts/0. I know it is possible because I do get graphical interface when the pendrive gets automatically mounted.
Thanking you,
Joe

sploot 10-01-2009 12:08 PM

I don't think I understand what you are trying to accomplish in your follow up questions. Can you explain your goal differently, and I will do my best to respond.

rg3 10-01-2009 12:47 PM

Quote:

Originally Posted by indiajoe (Post 3703719)
Hi,
Thanks a lot. I never realised such a vast possibility exists in udev. Now I started studying more about udev. One thing I couldn't figure out is how I can run commands like /usr/bin/kdialog from scripts run from udev. ( I am facing the same problem from crontab). Why is it not working even after I redirect it into >>/dev/pts/0. I know it is possible because I do get graphical interface when the pendrive gets automatically mounted.
Thanking you,
Joe

This doesn't quite work as you think it works. KDE is listening and receiving HAL events (that's apart from udev) and it pops a window up to notify you when a new device is inserted. Nothing is launched from a udev rule.

To be able to create a window from a udev rule you must set up a few things in the scripts you run, and I'm not really sure if it would really work. You have to set DISPLAY, to begin with, and probably you will want to use "su" to change to your home user so the GUI process is not running as root, etc.

indiajoe 10-03-2009 08:04 AM

Well, Then I shall use a flag..
 
Hi ,
Thanks again for the solution . It helped a lot..
What I wanted exactly was: How to run scripts which has graphical user interface from udev.
Since it is not easy to do that from udev, I understand the best method will be to keep a script running from the startup itself, and keep looking for some Flag file, as a signal to run the script having GUI. And use udev just to create that Flag file when I connect pendrives.

To be more specific on what I have to do : I have to run a guide to the contents in each cataloged pendrive, each time a user plugs it into the machine.
Am I in the right direction if I do as I mentioned in first paragraph?
Thanks a lot for giving me the idea how KDE does that using deamon.
-
Joe

rg3 10-03-2009 08:32 AM

Maybe you can add a custom KDE action that will be offered first in the menu when you insert one of those pendrives, provided you can identify somehow all of them. This is just an idea. I found some information here:

http://docs.kde.org/stable/en/kdebas...ons/index.html

speccy 10-03-2009 02:55 PM

You may be able to use inotifywait.

Code:

#!/bin/bash

while EVENT=$(inotifywait -q -e create /dev | awk '{print $1$3}'); do
        if [[ "$EVENT" =~ 'sd([a-z][0-9])' ]]; then
                kdialog --msgbox "$EVENT"
        fi
done



All times are GMT -5. The time now is 03:20 AM.