I am trying to write a udev rule for USB drive plugin. I want to first mount it to /media and then run the script at /usr/local/bin/script.sh. When I insert the USB, it doesn't even mount the device with this rule. However, If I manually type the part in RUN into terminal, It mounts and executes script.

The rule file says:

ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/bin/mount -t auto /dev/%k /media/ && /usr/local/bin/script.sh"

Is this the right way to execute two commands in RUN section of the rule?


It is not the right way to execute 2 commands. I do not like the matching part of your rule either, but I am going to leave it as it is for this example. You could do :

ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/bin/sh -c '/bin/mount -t auto /dev/%k /media/ && /usr/local/bin/script.sh'"


ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/bin/mount -t auto /dev/%k /media/", RUN+="/usr/local/bin/script.sh"

but in your case why won't you just move the mount command inside of your script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.