2

I have a simple udev rule to match USB storage devices which runs a simple script that looks for a file in the USB. I'm using the usbmount package in order to automatically mount the USB. As the documentation says, the USB devices will be mounted to /media/usb{0..7}.

When the USB storage device has been detected I run this script

#!/bin/bash

options=("usb0" "usb1" "usb2" "usb3" "usb4" "usb5" "usb6" "usb7")

for dir in "${options[@]}"
do
    lookup="/media/$dir/luah_v2_version.zip"

    if [ -e "$lookup" ]; then
        cp $lookup /home/luah
        exit 0
    else
        echo "could not find $lookup" >> /home/luah/log
    fi
done

The thing is that when the script is ran automatically after the USB was connected, it results in a log file where it says that the file could not be found in any of the directories above (while it does exist). When I then manually run the script it works and copies the zip file to where I wanted.

I've tried to set a wait time before the script executes of 10 seconds to see maybe the USB was not mounted by the time the script ran but I get the same end result.

  • Have you checked if /media/usb[0-7] is the actual path? The script works for me, and I can't see anything wrong with it (aside from the unquoted $lookup in cp $lookup /home/luah, but that is not the problem in this case) – kos Sep 13 '15 at 14:08
  • @kos Yes I've ensured many times that these directories exist. Also, please note that I mentioned in my question that when I run this script manually then it works just as expected – kfirba Sep 13 '15 at 14:10
  • Ah sorry, I missed that. Then I have no clue, +1 – kos Sep 13 '15 at 14:16
  • @kos that's frustrating :/ Did you manage to reproduce that on your end? – kfirba Sep 13 '15 at 14:22
  • I couldn't try with udev since I don't have an USB drive handy other then the one I'm booting from. However I'll try this later and let you know, since I want to start playing with udev as well, it might come handy – kos Sep 13 '15 at 14:48
2

Probably usb has not been mounted at the time your rule is processed. Try adding a hook to the usbmount i.e. create a file named /etc/usbmount/mount.d/10_autocopy.sh

#!/bin/sh
[ -f $UM_MOUNTPOINT/luah_v2_version.zip ] &&  cp $UM_MOUNTPOINT/luah_v2_version.zip /home/luah

then make it executable:

chmod +x /etc/usbmount/mount.d/10_autocopy.sh

variables set for mount scripts (/etc/usb/mount.d):

  • UM_DEVICE: mounted device
  • UM_MOUNTPOINT: mount point of the device
  • UM_FILESYSTEM: filesystem type
  • UM_MOUNTOPTIONS: mount options
  • UM_VENDOR: vendor string of the device
  • UM_MODEL: model or product string

variables set for unmount scripts (/etc/usb/umount.d):

  • UM_DEVICE: mounted device
  • UM_MOUNTPOINT: mount point of the device
  • UM_FILESYSTEM: filesystem type

Note:

  • all scripts are by default executed as user root.
  • usbmount uses the syslog facility if VERBOSE=yes - /etc/usbmount/usbmount.conf
|improve this answer|||||
  • How does this script get's executed? where does the $UM_MOUNTPOINT is being created? I guess that what you are trying to do is to add a hook that whenever usbmount is mounting a USB it will look into it with the script you wrote above? – kfirba Sep 13 '15 at 14:52
  • It doesn't seem to copy the file to the desired location. Any idea why? – kfirba Sep 13 '15 at 14:58
  • I just changed the name of the file to be 10_autocopy instead of autocopy.sh and it works. Thanks a lot! I was looking for any hooks that usbmount may trigger but there was no documentation so I didn't know about this. THANKS! – kfirba Sep 13 '15 at 15:03
  • It would be great if you could list the variables that are available in those scripts for reference. Also, under what user does the script run and does it have the PATH variable set anywhere else in the system? Does it also have an error log? – kfirba Sep 13 '15 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.