3

Can udev be used to trigger an action when an always-connected drive is mounted, or is it only useful for hotplugging (as the documentation suggests)?

I want to trigger a script when my always-connected SD card is mounted. This rule, placed inside /etc/udev/rules.d looks okay in syntax:

KERNEL=="mmcblk0",ACTION=="mount",RUN+="/path/to/script.sh"

But the script does not run when the drive is mounted.

Once again, there is no hotplugging and the drive is an SD card (hence the mmcblk0 device name).

If udev is not appropriate, what else might work?

4
  • I think you should check few things, 1. does your script have execute permission? 2. do you see mount action for the drive? if not refer linux.die.net/man/8/udevadm and see what event is spit out by udev when drive is mounted. if it is at startup, does udev starts before drive mounted ? Commented Dec 13, 2015 at 12:36
  • 1
    I am suggesting to you udevadm monitor to see events and then write rule Commented Dec 13, 2015 at 12:40
  • Thanks for the useful pointer! Turns out udev fires no mount event, just a bunch of add, remove and change. Will have to find a workaround. Commented Dec 13, 2015 at 12:46
  • 1
    sumid in this link askubuntu.com/questions/25071/… suggest using systemd, not sure if you have systemd or not. Commented Dec 13, 2015 at 13:06

1 Answer 1

4

The answer seems to be "yes, but". In my case at least, the ACTION=="mount" event does not fire when my already-connected SD card is mounted, but ACTION=="change" does. By running udevadm monitor -p I was able to identify a property (ID_FS_LABEL, the volume label) to make the rule work:

ENV{ID_FS_LABEL}="MyVolumeLabel",ACTION=="change",RUN+="/path/to/script.sh"

But it turns out that udev is not ideal for triggering anything other than short scripts, because they block it while completing. Instead I followed the advice suggested by AnkurTank and went with a systemd service, which works well.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.