Auto copy from usb

Hi all,
I’m very newbie in linux, I’m struggling with udev without success from several hours.

My goal is simple (I suppose): when a usb device is inserted (pendrive, microsd, hard disk, etc.), osmc (udev?) should run a script.
This script will check if a certain folder is present. If so, all files whitin this folder will be copied in osmc. With some “kodi-send notification” to inform user about start and finish copy.

I’m in the very first part: execute a script when a pendrive is inserted. No way to execute my (debug) script. And I don’t know if udev is or not working.

Please, do you have any suggestion?

Have a look at this: How to Run a Script When USB Devices Is Attached or Removed Using UDEV – The Geek Diary It seems close to what I’d have recommended, but goes into more detail

I’m not sure if it’ll work with an SD card but you can experiment. Unfortunately, I’ll be physically away from an OSMC device this weekend so won’t be able to try anything myself.

First of all, thanks for the answer.
I’ve followed the guide, but nothing changed. Probably I’m doing something wrong.
Here my steps:

osmc@osmc:~$ udevadm monitor --kernel --property --subsystem-match=usb
monitor will print the received events for:
KERNEL - the kernel uevent

KERNEL[195.302412] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3 (usb)
ACTION=add
BUSNUM=001
DEVNAME=/dev/bus/usb/001/006
DEVNUM=006
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3
DEVTYPE=usb_device
MAJOR=189
MINOR=5
PRODUCT=1b1c/1a03/110
SEQNUM=1366
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[195.305545] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0
DEVTYPE=usb_interface
INTERFACE=8/6/80
MODALIAS=usb:v1B1Cp1A03d0110dc00dsc00dp00ic08isc06ip50in00
PRODUCT=1b1c/1a03/110
SEQNUM=1367
SUBSYSTEM=usb
TYPE=0/0/0

So I’ve changed my udev rules like this:

sudo nano /etc/udev/rules.d/50-autotransfer.rules

ACTION==“add”, SUBSYSTEM==“usb”, ENV{PRODUCT}==“1b1c/1a03/110”, RUN+=“/bin/su osmc --command=‘/home/osmc/autocopy.sh’”

Tried with this too:

ACTION==“add”, SUBSYSTEM==“usb”, ENV{PRODUCT}==“1b1c/1a03/110”, RUN+=“/home/osmc/autocopy.sh”

(I reboot everytime, to be sure udev daemon updates)

My script (for debug purpose):

/home/osmc/autocopy.sh

#!/bin/bash
vcgencmd display_power 0
echo “hello” > /home/osmc/hello.txt

(just to see if display goes off and hello.txt file is created)

Everytime I insert my pendrive, nothing happens. Only some bad words travel in my mind (I don’t want my son to hear).

So, please, any (other) help will be much appreciated.

No need to reboot, just do sudo udevadm control --reload-rules to reload and sudo udevadm trigger to retrigger.

And sudo udevadm monitor should show you what happens or doesn’t happens when you plugin the USB stick.

Try using change instead of add?
Also check script permissions

thanks, I’ll do so.

yep, it shows me information when I plug and unplug the pendrive.

Tried “change” instead of “add”… nothing change

Script permissions are 755

So… what else?

An alternative solution would be post-mount commands in a UDisks glue configuration.

Two possibilities I can think of:

  1. Remove ENV{PRODUCT}==“1b1c/1a03/110” from your udev rule. When debugging, it’s usually easier if you start small. If that works, add things until it breaks.
  2. Rename the rules file to 01-autotransfer.rules so it’ll be run early on.

Edit: Your test script tries to do two things:

vcgencmd display_power 0
echo “hello” > /home/osmc/hello.txt

Remove the vcgencmd command. Using the KISS principle, it’s unnecessary and will most likely fail since you didn’t specify the full path (which is /opt/vc/bin/vcgencmd). And just to be 100% watertight, use /bin/echo in the second line.

Never heard about it, I’ll investigate… but I wish to know why the udev thing doesn’t work.

Ok, all done: removed ENV{PRODUCT} (I had already done it), renamed to 01-autotransfer.rules, changed content as suggested (I didn’t know what KISS was, now I know and I really feel like the second “S”)

Reloaded rules, attached pendrive… hello.txt doesn’t show up!

My actual configuration is:

01-autotransfer.rules:

ACTION==“add”, SUBSYSTEM==“usb”, RUN+=“/home/osmc/autocopy.sh”

autocopy.sh:

#!/bin/bash
/bin/echo “hello” > /home/osmc/hello.txt

Well what was your findings from udevadm monitor? Is the stick inserting recognized?
Next step would be changing the logging level.

Here udevadm monitor output:

osmc@osmc:~$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[3845.592682] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3 (usb)
KERNEL[3845.593022] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
KERNEL[3845.593581] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0 (scsi)
KERNEL[3845.593764] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/scsi_host/host0 (scsi_host)
UDEV  [3845.602413] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3 (usb)
UDEV  [3845.607571] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
UDEV  [3845.610743] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0 (scsi)
UDEV  [3845.615549] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/scsi_host/host0 (scsi_host)
KERNEL[3846.631559] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0 (scsi)
KERNEL[3846.631757] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0 (scsi)
KERNEL[3846.631872] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0 (scsi_disk)
KERNEL[3846.632008] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/scsi_device/0:0:0:0 (scsi_device)
KERNEL[3846.633151] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/scsi_generic/sg0 (scsi_generic)
KERNEL[3846.634194] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/bsg/0:0:0:0 (bsg)
KERNEL[3846.636180] add      /devices/virtual/bdi/8:0 (bdi)
UDEV  [3846.638744] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0 (scsi)
UDEV  [3846.642178] add      /devices/virtual/bdi/8:0 (bdi)
UDEV  [3846.645383] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0 (scsi)
UDEV  [3846.649850] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0 (scsi_disk)
UDEV  [3846.651560] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/scsi_device/0:0:0:0 (scsi_device)
UDEV  [3846.654543] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/bsg/0:0:0:0 (bsg)
UDEV  [3846.655436] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/scsi_generic/sg0 (scsi_generic)
KERNEL[3846.705716] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/block/sda (block)
KERNEL[3846.705986] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/block/sda/sda1 (block)
UDEV  [3846.835051] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/block/sda (block)
UDEV  [3846.947429] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host0/target0:0:0/0:0:0:0/block/sda/sda1 (block)

Also udevadm monitor --kernel --property --subsystem-match=usb output:

osmc@osmc:~$ udevadm monitor --kernel --property --subsystem-match=usb
monitor will print the received events for:
KERNEL - the kernel uevent

KERNEL[3875.802504] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3 (usb)
ACTION=add
BUSNUM=001
DEVNAME=/dev/bus/usb/001/010
DEVNUM=010
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3
DEVTYPE=usb_device
MAJOR=189
MINOR=9
PRODUCT=1b1c/1a03/110
SEQNUM=2133
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[3875.805846] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
ACTION=add
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0
DEVTYPE=usb_interface
INTERFACE=8/6/80
MODALIAS=usb:v1B1Cp1A03d0110dc00dsc00dp00ic08isc06ip50in00
PRODUCT=1b1c/1a03/110
SEQNUM=2134
SUBSYSTEM=usb
TYPE=0/0/0

Run this:

udevadm test -a add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3

If the udev rule is working, you should see something like run: /home/osmc/autocopy.sh in the output.

Yes, nearby the end I see run: '/bin/bash /home/osmc/autocopy.sh'

But I also have some (working) updates:

My actual configuration is:

01-autocopy.rules:
ACTION=="add", SUBSYSTEM=="usb", RUN+="/bin/bash /home/osmc/autocopy.sh"

autocopy.sh:
echo "hello" > /home/osmc/hello.txt

Now hello.txt file is created when I insert pendrive.

So, what was the problem? Only to explicit /bin/bash in RUN section?

To me that would suggest that autocopy.sh wasn’t world executable.

755 from beginning of the story

Anyway, let’s suppose udev is now working (and, as for so much things, without know why), I would to copy the content of “TO_COPY” folder, if present (on the usb device, I mean), to /home/osmc/pictures.

What’s the best practise in this case?

I think your main challenges will be (1) to ensure that the device has been mounted before you check its contents and (2) to know what mountpoint udisks has used.

This might well link back to what Sam wrote earlier:

Fortunately, I have very little knowledge of udisks glue. :wink:

Fortunately, me too. :sweat_smile:

My 2 cents: under /media I see a folder for every usb device (I’ve tried with 2 pendrive and 1 hard disk). Would be simple to loop for each folder, if TO_COPY folder is inside, copy all content to /home/osmc/pictures

I mean: would be simple to someone can do it, not me!

Something along the lines of:

for dirname in /media/*; do
    echo "$dirname"
    #check for TO_COPY, etc.
done