Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command after mount/unmout usb pen drive

I have Raspberry Pi (with Raspbian) and using it as DLNA/UPnP server and renderer. I run minidlna as DLNA server and i have some media files on USB.

I would like to automaticaly rebuild DLNA DB when drive is mounted and unmounted. This is done by command:

sudo service minidlna force-reload

Is threre any way how to autorun this command?

BTW I use "USBmount" package for automount USB drives.

Thanx

like image 257
dosdroid Avatar asked Mar 21 '23 05:03

dosdroid


2 Answers

You can do this using the tool usbmount. It has the possibility to add scripts that will be run on mount/umount events in /etc/usbmount/mount.d/ and /etc/usbmount/umount.d/.

like image 53
danielenick89 Avatar answered Mar 29 '23 03:03

danielenick89


Start by finding your device in lsusb. Note the ID (eg 12f5:a91a)

Create a new udev rules file in /etc/udev/rules.d/ eg /etc/udev/rules.d/100-my-mount.rules and write a new rule in there like this:

ACTION=="add", ATTRS{idVendor}=="12f5", ATTRS{idProduct}=="a91a", RUN+="/home/your_username/bin/my-mount-script.sh"

For unmounted device use ACTION=="remove" in rule and another script.

like image 20
Pavel Stárek Avatar answered Mar 29 '23 03:03

Pavel Stárek