On Ubuntu 12.04, my rule fires a script called motoe.sh when a Motorola Moto E is connected to the machine USB port. My custom rule does not work.

The rule is:

ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="22b8", ATTRS{idProduct}=="2e76", RUN+="/usr/local/bin/motoe.sh"

motoe.sh is:


motoemount.sh & motoedesktop.sh

motoemount.sh is:


go-mtpfs /media/MyAndroid

motoedesktop.sh is:


gmessage -center -timeout 3 -buttons OK:0 "message to "

rdesktop -u username -d domain -r disk:motoe=/media/MyAndroid/Interno/Download ip_address -a 8 -k pt-br

gmessage -center -timeout 3 -buttons OK:0 "another message to the user" &
umount /media/MyAndroid/

All the .sh scripts have the execution permission set.

I tried to use SUBSYSTEM, SUBSYSTEMS, with DRIVER without DRIVER, and no effect.

I don't figure what I missed out. If I execute motoe.sh from a terminal, it runs correctly.

  • I tried without the gmessage, but result in no effect. I tried to point directly to motoemount.sh, but I still get the same result. Thank your for your guidance. – JPDev Feb 2 '16 at 21:43
  • Glad I could help a bit, JPDev. (A) Feedback on what the problem(s) was/were is not obligatory, but would be nice in the form of a concise but pregnant comment to my answer. (B) I flagged yr last message "It's working now. Cbhihe, thank you again for your time and answer, it helped me a lot." because you published it as an answer when it really should be a comment. AU frowns upon that. We try to keep things simple and you should expect it to be deleted. --- Cheers. – Cbhihe Feb 4 '16 at 8:23
  • Sorry for my mistake, I'm new to AskUbuntu, it'll not happen again. – JPDev Feb 4 '16 at 10:26
  • No worries at all JPDev. I was just highlighting some of the dos and don'ts, knowing you propably weren't aware of them. ;-) AU always welcome newcomers. – Cbhihe Feb 4 '16 at 10:41

-1- Be careful when you run an external program as udev execution will be suspended during the external program's execution. Better said, it will wait until the external program exits before proceeding. You can still execute a long winded program but you must "detach" it immediately, by running it in the background. In yr case, yr motoe.sh should probably read:

} &

which is equivalent to { motoemount.sh; motoedesktop.sh }&, i.e. executing motoedesktop.sh unconditionnally after motoemount.sh has exited.

Note that :

$ motoemount.sh &; motoedesktop.sh

means that motoemount.sh and motoedesktop.sh are executed asynchronously, i.e. motoedesktop.sh starts executing before motoemount.sh even exits.

$ motoemount.sh && motoedesktop.sh

is not the same. In that case, motoedesktop.sh executes ONLY if the exit value of motoemount.sh is "0".

[....] If I execute motoe.sh from a terminal, it runs correctly

-2- udev does not run external programs on any active terminal, and it does not execute them under the context of a shell. Be sure to ensure your program is marked executable, if it is a shell script ensure it starts with an appropriate shebang (e.g. #!/bin/sh). The environment that udev sees will not be the same environment you (or whoever $USER) is gets on the console. So specify absolute paths for all your commands:

motoemount.sh -> /path/to/motoemount.sh
motoedesktop.sh -> /path/to/motoedesktop.sh
go-mtpfs -> ...
gmessage -> ...
rdesktop -> ...
umount -> /bin/umount

In order to find a path for system cmds (such as umount) you can use the cmd which [cmd] without brackets, e.g.:

$ which umount

-3- Verify that yr process has sufficient privileges to unmount /media/MyAndroid/

[....] If I execute motoe.sh from a terminal, it runs correctly

-4- If you print to screen (not sure you do, as I'm not familiar with gmessage but it looks like you do), make sure your DISPLAY environment variable is set (known to udev) and exported (passed on to sub-shells). Otherwise do not expect any standard output to appear on your terminal. For an example see this, in particular the # set DISPLAY section.

  • Cbhihe, thank you for your time and detailed answer to my question. I made some changes. All the scripts are marked as executable, for test purpouses I removed the gmessage, but still not working. – JPDev Feb 3 '16 at 11:20

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.