Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

usb autolaunch

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help!
View previous topic :: View next topic  
Author Message
wyliecoyoteuk
LXF regular


Joined: Sun Apr 10, 2005 11:41 pm
Posts: 3443
Location: Birmingham, UK

PostPosted: Sun Apr 10, 2005 11:50 pm    Post subject: usb autolaunch Reply with quote

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8454
Location: Warrington, UK

PostPosted: Mon Apr 11, 2005 12:15 am    Post subject: RE: usb autolaunch Reply with quote

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
View user's profile Send private message
evilnick
Moderator


Joined: Mon Apr 04, 2005 12:47 pm
Posts: 151
Location: LXF towers

PostPosted: Mon Apr 11, 2005 12:17 am    Post subject: RE: usb autolaunch Reply with quote

You can also set up a custom Hotplug script to do this, but udev is better
Back to top
View user's profile Send private message
Guest






PostPosted: Mon Apr 11, 2005 10:36 pm    Post subject: RE: usb autolaunch Reply with quote

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
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8454
Location: Warrington, UK

PostPosted: Mon Apr 11, 2005 11:33 pm    Post subject: RE: usb autolaunch Reply with quote

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)
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Apr 12, 2005 2:38 pm    Post subject: RE: usb autolaunch Reply with quote

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
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8454
Location: Warrington, UK

PostPosted: Tue Apr 12, 2005 2:55 pm    Post subject: RE: usb autolaunch Reply with quote

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.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
wyliecoyoteuk
LXF regular


Joined: Sun Apr 10, 2005 11:41 pm
Posts: 3443
Location: Birmingham, UK

PostPosted: Tue Apr 12, 2005 11:19 pm    Post subject: RE: usb autolaunch Reply with quote

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 user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help! All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast