| View previous topic :: View next topic |
| Author |
Message |
wyliecoyoteuk LXF regular

Joined: Sun Apr 10, 2005 11:41 pm Posts: 3358 Location: Birmingham, UK
|
Posted: Sun Apr 10, 2005 11:50 pm Post subject: usb autolaunch |
|
|
Hi, y`all I`m back!
The new website is excellent so far, I stopped droppingin because it was soooo slow.
Anyway, is it possible to launch a script when a USB device is connected?
I have an external USB HDD that Suse9.2 recognises and automounts, but I would like to automate backups to it.
i.e. I would like to run a script like :
cd /home
tar -cf -. |(cd /media/<usbbdrive>: tar xvf-)
to copy the contents of my home directory when I plug it in to a particular USB port. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Mon Apr 11, 2005 12:15 am Post subject: RE: usb autolaunch |
|
|
You can do this is you use udev. First set up a rule to give the device a unique, persistent name (see this month's mag for details). Then put a script in /etc/dev.d. the script must have the suffix .dev and could be somethng like
| Code: | #!/bin/sh
if [ "$DEVNAME" == "backup" ]; then
tar -cf -. |(cd /media/<usbbdrive>: tar xvf-)
fi |
|
|
| Back to top |
|
 |
evilnick Moderator

Joined: Mon Apr 04, 2005 12:47 pm Posts: 151 Location: LXF towers
|
Posted: Mon Apr 11, 2005 12:17 am Post subject: RE: usb autolaunch |
|
|
| You can also set up a custom Hotplug script to do this, but udev is better |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Apr 11, 2005 10:36 pm Post subject: RE: usb autolaunch |
|
|
makes sense, but how is the $DEVNAME parsed?
Suse 9.2 assigns persistent names by default, my usb drive is file:/media/usb-DEF1098C08E5:0:0:0p1
whatever port I plug it in.
unfortunately, it is not obvious where the script should be.
there are 2 scripts in /etc/dev.d/block/
they are 50-hwscan.dev and 51-subfs.dev
i tried adding usbhdd.dev, but no result.
the script works if excecuted, but it seems to be a problem launching it automatically.
I might use a cron job to run it, but the auto option would be better |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Mon Apr 11, 2005 11:33 pm Post subject: RE: usb autolaunch |
|
|
DEVNAME is set to the name of the device before the script is called.
That name is the mount point, not the device name, which will start with /dev/ $DEVNAME is relative to /dev. You need to set up a rule in /etc/udev/rules/ to ensure the device always has the same device name. If you don't it will be sda if you plug it in first but sdb if you connect another USB storage device first.
All scripts in /etc/dev.d are run when a device is connected, so it shouldn't matter what you call it. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Apr 12, 2005 2:38 pm Post subject: RE: usb autolaunch |
|
|
well that worked:)
Unfortunately, it looks like it runs the script on both mount and umount!
shown as guest `cos on work PC |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Tue Apr 12, 2005 2:55 pm Post subject: RE: usb autolaunch |
|
|
udev aslo sets ACTION to add* or remove*
| Code: | | if [ "$DEVNAME" == "backup" -a "$ACTION="add*" ]; then |
should do it. If not, read the udev man page for more details. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
wyliecoyoteuk LXF regular

Joined: Sun Apr 10, 2005 11:41 pm Posts: 3358 Location: Birmingham, UK
|
Posted: Tue Apr 12, 2005 11:19 pm Post subject: RE: usb autolaunch |
|
|
Got it working:)
script as follows :
#!/bin/sh
if [ "$DEVNAME" == "/dev/sda1" -a "$ACTION" == "add" ] ; then
rsync -av /home /media/usb-DEF1098C08E5:0:0:0p1
fi
I had to add:
export $ACTION
to the end of /etc/dev.d/51-subfs.dev
to make sure it passed the variable.
(and I decided to use rsync , much tidier)
Suse9,2 assigns fixed names including the serial number of the device by default, and I only want to use one device on this machine (it is a headless scan storage server), just want to be able to plug in a big USB drive and automatically copy the directory tree under /home/scanroute.
Thanks, nelz! |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|