I want my Ubuntu 15.04 x64 server system to execute a script whenever a certain flash drive is inserted. So I created a custom rule file with the following content
ACTION=="add",SUBSYSTEM=="usb",ATTR{idVendor}=="my-vendor",ATTR{idProduct}=="my-id",RUN+="/var/scripts/my-script.sh"
The script executed whenever the flash drive is inserted, but I have two problems with it:
1) The script is apparently not executed when the computer is (re-)started with the drive already inserted. Is there any way to execute the script after startup if the flash drive is already connected?
2) The script wants to mount the device, but If I try to mount the device like
mount LABEL=mylabel /some/path
or
mount UUID=my-uuid /some/path
The source cannot be found. Currently I inserted a
sleep 5s
into my script, but this does not seem like a reliable solution. Is there any (failure tolerant) way to wait for the system to be sufficiently initialized?
For testing purposes, the script only contained
#!/bin/sh
date >> /tmp/test.log
date >> /tmp/test.log
, (in addition to the shebang) and if I rebooted the system while the pen drive was still inserted, no entry was made./tmp
is not yet mounted read-write at that point. At least on Debian jessie with sysvinit,/
is still mounted read-only and filesystems from/etc/fstab
aren't mounted yet at the timeudevadm trigger --action=add
runs. The robust way on systemd would be to add a unit with the right dependencies, I don't know how to do that.