How can I trigger a script to be run when an always connected drive is mounted?
I have tried udev
, specifically placing a rule 99-local.rules
in /etc/udev/rules.d
:
KERNEL=="mmcblk0",ACTION=="mount",RUN+="/path/to/script.sh"
And reloading the rules. But the script does not run when the drive is mounted.
Note that the drive is an SD card, hence the mmcblk0
block device name. It is not a USB drive and is not being hotplugged.
Turns out that this is not easy to accomplish with
udev
. Withudevadm monitor -p
I verified that, when mounted, my SD card fires noACTION=="mount"
event inudev
. I read somewhere that themount
event has been deprecated or removed, which if true would explain it.udev
, after all, is supposed to manage hotplugging.In addition,
udev
-triggered scripts must complete very quickly because they blockudev
while running. Not ideal for a substantial task, for example backup.I achieved my aim by writing a
systemd
service file in/etc/systemd/system/
usingExecStart
for.mount
:Works correctly, though it does require specifying the mount point.