2

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.

3
  • 1
    The script does not run, probably because the udev rule is overlooked. It is probably overlooked because in principle yr sd card is mounted automatically under /media/yr-username/yr-mmcbki0-tag Confirm or correct me if I am wrong by checking whether yr sd card is mounted or not under /media./.... Run mount -l | grep mmcblk0 and post result. Depending on yr response I may have a working solution tomorrow.
    – Cbhihe
    Dec 13, 2015 at 21:24
  • Thanks for the suggestion! But there do seem to be deeper issues with using udev in this particular case. Detail in answer.
    – Sqerstet
    Dec 15, 2015 at 13:35
  • Sorry, I just saw yr comment now because I was not notified (i.e. you failed to include @Cbhihe in yr comment to my comment). Glad however you could get it to work.
    – Cbhihe
    Dec 15, 2015 at 16:32

1 Answer 1

6

Turns out that this is not easy to accomplish with udev. With udevadm monitor -p I verified that, when mounted, my SD card fires no ACTION=="mount" event in udev. I read somewhere that the mount 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 block udev 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/ using ExecStart for .mount:

[Unit]
Description=Script to run when drive mounted at /path/to/Stuff
Requires=path-to-Stuff.mount
After=path-to-Stuff.mount

[Service]
ExecStart=/path/to/script.sh
Type=forking

[Install]
WantedBy=path-to-Stuff.mount

Works correctly, though it does require specifying the mount point.

3
  • Could you post your complete service file in you answer? I want to learn from the way you did it.
    – user.dz
    Dec 24, 2015 at 20:12
  • @Sneetsher Done.
    – Sqerstet
    Dec 25, 2015 at 13:39
  • You're my hero - upvoted
    – Michael
    Jan 23, 2017 at 3:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .