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
  • 1
  • 4

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
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
  • 49,176