Sometimes it might be nice to have a script run automatically when a specific USB device gets plugged in.
So long as you are running a system based on udev, this is very simple.
The simplest situation is where you can identify the USB device ID for a particular device (or family of devices) and trigger some action based on that (which might then filter out only certain very specific devices from that family, and otherwise do nothing).
For example, I have two Tolino Epos eBook readers, and I wanted to set up a mechanism where they automatically synchronised themselves from my eBook collection whenever they were plugged in.
The USB device ID for a Tonile Epos eBook reader is 1f85:6053, so I create a file containing:
ATTRS{idVendor}=="1f85",ATTRS{idProduct}=="6053",RUN+="/usr/local/sbin/tolinosync.sh"
The script /usr/local/sbin/tolinosync.sh can do anything you want - in my case it is:
#!/bin/bash # Wait for a device on the USB to settle, then mount it, sync it, and unmount it debug=0 [ "$debug" -gt "0" ] && logger -t Tolino "Started by udev" [ "$ID_FS_LABEL" = "tolino" ] || exit 0 [ "$debug" -gt "0" ] && logger -t Tolino "It's a Tolino :)" [ "$DEVPATH" = "${DEVPATH/block}" ] && exit 0 [ "$debug" -gt "0" ] && logger -t Tolino "It's a block device" [ "$ACTION" = "change" ] || exit 0 [ "$debug" -gt "0" ] && logger -t Tolino "It's remounting" [ -n "$DISK_MEDIA_CHANGE" ] && exit 0 ( logger -t Tolino "I have found device $DEVNAME and I am mounting it on /mnt" mount $DEVNAME /mnt [ "$debug" -eq "0" ] && books=`rsync -vac /home/me/eBooks/ /mnt/Books/` [ "$debug" -gt "0" ] && books=`rsync -Pvac /home/me/eBooks/ /mnt/Books/` [ "$debug" -gt "0" ] && echo "$books" >>/tmp/tolino.rsync.txt umount /mnt logger -t Tolino "I mounted device $DEVNAME, synced the books to it, and unmounted it again" echo -e "$books\n\nTolino has been updated and can be unplugged." | mail -s "Tolino update" me ) &
Replace me in three places in the above script with your own username, and perhaps adjust /home/me/eBooks also accordingly.
Now, whenever I plug one of my eBook readers into the machine, it automatically gets mounted, new books are copied to it, it gets unmounted again (so I can safely unplug it) and the script sends me an email telling me what it's done.
I have a similar script for a different USB device ID for a USB key (which is a storage device literally in the shape of a key, complete with a hole to mount it on a keyring, so I carry it with me everywhere) which syncs my professional work and email directories, so that:
Go up
Return to main index.