3

I need to automatically run my script /var/www/html/configWWW when any USB is plugged in to my Rasperry.

UDEV RULE - /etc/udev/rules.d/myRule.rules

ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="****",
ATTR{idProduct}=="****", RUN+="/var/www/html/configWWW"

MY SCRIPT /var/www/html/configWWW

#!/bin/bash
file="/media/pi/`ls /media/pi`/SymSif.xml"
if [ -f "$file" ]
then
        (
        echo "it works: $(date)" >> /home/pi/Desktop/test.txt
        )
else
        (
        echo "it does not works: $(date)" >> /home/pi/Desktop/test.txt
        )
fi

On the other hand, if i run script from bash bash /var/www/html/configWWW, it works!

Why doesn't my udev rule work like my bash command?

Daniel
  • 31

2 Answers2

2

There is a problem running scripts from udev, very little survives when the sandbox is discarded (mounts, network connections ...). The second reason, scripts running from udev will block all further events for this or a dependent device and will eventually timeout and all running processes started by the script will be unconditionally terminated.

You can get the same result by using systemd:

$ cat /etc/systemd/system/garmin.service
[Unit]
Description=Autorun actions for Garmin FR620
Requires=dev-disk-by\x2did-usb\x2dGarmin_FR620_Flash\x2d0:0.device
After=dev-disk-by\x2did-usb\x2dGarmin_FR620_Flash\x2d0:0.device

[Service]
ExecStart=/opt/bin/autorun_garmin_fr620.sh

[Install]
WantedBy=dev-disk-by\x2did-usb\x2dGarmin_FR620_Flash\x2d0:0.device

You can find your .device unit by connecting your device and run systemctl --all list-units

don´t forget to enable your .service:

$ systemctl enable garmin.service


Or you could start your systemd .service from the udev rule:

$ cat /etc/udev/rules.d/99-garmin-autorun.rules
# Start garmin FR620 systemd .service
ACTION=="add", \
KERNELS=="3-3", \
SUBSYSTEMS=="usb", \
ATTRS{idProduct}=="2657", \
ATTRS{idVendor}=="091e", \
TAG+="systemd", ENV{SYSTEMD_WANTS}="garmin.service"

...and the systemd .service could look something like this:

[Unit]
Description=Autorun actions for Garmin FR620

[Service]
Type=oneshot
ExecStart=/opt/bin/autorun_garmin_fr620.sh
  • Wow. I had been fighting udev for a long time trying to get it to automatically move/rename photos from my field cam using exiftool. Using systemd instead just works! – user162657 Aug 16 '22 at 19:04
1

Better if you could add udevadm info -a ... to the post. Anyway, here few things I expect:

  • Set the script executable permission bit.
  • Change the rule filename to standard format, example 99-alpha.rules
  • Check out the difference between SUBSYSTEM and SUBSYSTEMS, and between ATTR and ATTRS.

See man udev

user.dz
  • 48,105
  • 1
    I don't think having a number in the name matters: Files in there are parsed in alpha order, as long as the name ends with ".rules". -debian wiki: udev – ki9 Jan 24 '20 at 05:14
  • 1
    @Keith I wouldn't trust such online resumed documentation versus original/upstream one. man udev has it clear All rules files are collectively sorted and processed in *lexical order*, regardless of the directories in which they live. However, files with identical filenames replace each other. Files in /etc have the highest priority, files in /run take precedence over files with the same name in /lib. . You can also verify it using udevadm test ... like in this question https://askubuntu.com/q/1144508/26246 . You are right about this case, myRule.rules is ordered last anyway. – user.dz Jan 24 '20 at 14:01
  • 1
    I think I see. You're saying that having a number in the filename isn't a requirement, just a recommendation, but that since most rules have a number, any of them could override your rule because the numbered rules will be processed first. – ki9 Apr 11 '20 at 03:55
  • 1
    @Keith, Number is a recommendation to control when to run your rules within a chain of other rules. "override" in udev has multiple direction (a rule can affect previous and next rules). Examples: (1) Last rule can override actions from previous rules. (2) First rule can override environment variables, tags that may affect next rules decisions/conditions. – user.dz Apr 11 '20 at 07:40