usb autolaunch

The place to post if you need help or advice

Moderators: ChrisThornett, LXF moderators

usb autolaunch

Postby wyliecoyoteuk » Sun Apr 10, 2005 10:50 pm

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.
User avatar
wyliecoyoteuk
LXF regular
 
Posts: 3456
Joined: Sun Apr 10, 2005 10:41 pm
Location: Birmingham, UK

RE: usb autolaunch

Postby nelz » Sun Apr 10, 2005 11:15 pm

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: Select all
#!/bin/sh

if [ "$DEVNAME" == "backup" ]; then
    tar -cf -. |(cd /media/<usbbdrive>: tar xvf-)
fi
User avatar
nelz
Site admin
 
Posts: 8498
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: usb autolaunch

Postby evilnick » Sun Apr 10, 2005 11:17 pm

You can also set up a custom Hotplug script to do this, but udev is better
User avatar
evilnick
Moderator
 
Posts: 151
Joined: Mon Apr 04, 2005 11:47 am
Location: LXF towers

RE: usb autolaunch

Postby Guest » Mon Apr 11, 2005 9:36 pm

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
Guest
 

RE: usb autolaunch

Postby nelz » Mon Apr 11, 2005 10:33 pm

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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8498
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: usb autolaunch

Postby Guest » Tue Apr 12, 2005 1:38 pm

well that worked:)
Unfortunately, it looks like it runs the script on both mount and umount!

shown as guest `cos on work PC
Guest
 

RE: usb autolaunch

Postby nelz » Tue Apr 12, 2005 1:55 pm

udev aslo sets ACTION to add* or remove*

Code: Select all
if [ "$DEVNAME" == "backup" -a "$ACTION="add*" ]; then


should do it. If not, read the udev man page for more details.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8498
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: usb autolaunch

Postby wyliecoyoteuk » Tue Apr 12, 2005 10:19 pm

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!
User avatar
wyliecoyoteuk
LXF regular
 
Posts: 3456
Joined: Sun Apr 10, 2005 10:41 pm
Location: Birmingham, UK


Return to Help!

Who is online

Users browsing this forum: No registered users and 3 guests