How i can execute script after mount usb device

Hi all.

I am new and i need make raspberry pi tv to autoplay all USB content. OSMC can auto mount USB and this work fine, when my raspberry boot with USB memory my start and play all movies from USB in loop, but i need make script which run when USB memory will mount.
I need this script to autoplay new content from USB drive without restart raspberry.

If someone know how i can make this please help me.


The best way is probably to create an udev rule.

I don’t think it’s a good idea to assume that all USB drives will contain playable media, so I’d advise you to choose one or two devices that you’ll use for this task. If they are all from different manufacturers, so much the better.

In directory /etc/udev/rules.d create a file 10-usb-play.rules and add this line:

ACTION=="add", KERNEL=="usb", SUBSYSTEM=="usb", ATTRS{idVendor}=="0951", ATTRS{idProduct}=="168e", RUN+="/home/osmc/"

NB it must all be on one line.

You will need to change the vendor and product IDs, plus the name of the script that you want to run. The IDs here are for a Kingston 32 GB USB drive. To see the correct vendor and product ID for your drive, use the lsusb command. (If not present, install the usbutils package using apt-get.) Example:

osmc@osmc:/etc/udev/rules.d$ lsusb
Bus 001 Device 019: ID 0951:168e Kingston Technology 
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

It might also be possible to use ATTRS{serial}==“000000000000000000000” using the real serial number of the device to narrow it down to a single device, if necessary. (Though I’ve not tested it with this rule.)

BUT be aware that udev RUN should only be used for short-running jobs. If you search on google for “udev long running job” you will see ways to get around this limitation.

Edit: Forgot to add that if udev doesn’t see the changes you might need to run either sudo systemctl restart udev or just reload the rules using:

sudo udevadm control --reload-rules
sudo udevadm trigger --action=change

Or just reboot. :wink:

1 Like