I have a USB device (POV camera) that collects a lot of data a fills up its storage regularly. The goal is to write a script that:
- Listens for the device to connect via USB
- Mounts it as a storage device
- Downloads data from the device
- Safely unmounts the device for removal (upon completion of the download)
I can add to the script later the option for blinking LEDs attached to certain pins on the GPIO to indicate status...
I've searched for "how to automatically download data from a usb device" on ubuntu and other linux sites, but with no results. Can anyone provide a starting point? I'm comfortable with writing code from documentation and examples.
2 Answers 2
Use udev rules.
find your device information.
udevadm -a -p /dev/path/device/
KERNELS=="1-3"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
...
ATTRS{quirks}=="0x0"
ATTRS{authorized}=="1"
ATTRS{manufacturer}=="SANDisk"
ATTRS{product}=="USB DISK"
ATTRS{serial}=="SD71011000019113"
Then create your udev rules file for your device. When creating rules file, use information you got from udevinfo command.
content of /etc/udev/rules.d/99-mydevice.rules
SUBSYSTEMS=="usb", ATTRS{serial}=="SD71011000019113", RUN+="/home/gurcan/sync.sh"
Create your script that will run as USB device connected
#!/bin/bash
#
rsync -avz /media/disk/photos/ /data/photos/
Reload udev rules
udevcontrol reload_rules
Test it. unplug/plug
-
1I tried to run udevinfo, but got a
-bash: udevinfo: command not found
response from the ssh shell. I checkedman
and there is aman
entry forudev
, but not forudevinfo
. I will likely try to pursue this withudev
... your answer was helpful though, so thanks! I'll look around to check onudevinfo
too, maybe I haven't installed a repository or something.user3.1415927– user3.14159272014年07月12日 01:30:05 +00:00Commented Jul 12, 2014 at 1:30 -
1@user3.1415927 Because of udevinfo replaced by udevadm in debian, i updated my answer.gurcanozturk– gurcanozturk2014年07月12日 08:22:52 +00:00Commented Jul 12, 2014 at 8:22
If you don't like udev, you can take a look on devmon.
It allows these options:
--exec-on-device DEVICE "COMMAND" Execute COMMAND after mounting DEVICE
--exec-on-label "LABEL" "COMMAND" Execute COMMAND after mounting LABEL
--exec-on-video "COMMAND" Execute COMMAND after video DVD mount
--exec-on-audio "COMMAND" Execute COMMAND after audio CD insertion
--exec-on-disc "COMMAND" Execute COMMAND after data CD/DVD mount
--exec-on-drive "COMMAND" Execute COMMAND after drive mount
--exec-on-unmount "COMMAND" Execute COMMAND after unmount
--exec-on-remove "COMMAND" Execute COMMAND after drive removal
Where the following in COMMAND will be replaced with:
%d mount point directory (eg /media/cd)
%f device name (eg /dev/sdd1)
%l label of mounted volume
Multiple --exec-on-XXX options may be used to execute multiple commands.
Other exec-on-XXX commands are ignored if exec-on-device or -label executed.