[arch-general] udev rules 2 times exec script
Hi, I try configure the rule in udev for when I connect specific usb device, launch script backup. I have this rule: /etc/udev/rules.d/10.autobackup.rules SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", ATTRS{idProduct}=="6387", SYMLINK+="external%n", RUN+="/usr/local/bin/autobackup.sh" This works well, buth exec 2 times the script... Jun 12 13:53:09 archlaptop autobackup.sh: BEGIN SCRIPT Jun 12 13:53:13 archlaptop autobackup.sh: BEGIN SCRIPT I googled and I see this: The issue was resolved by adding ENV{DEVTYPE}=="usb_device" in a rule. If add ENV{DEVTYPE}=="usb_device" and change the SUBSYSTEM=="usb_device" , when plug usb, udev does nothing ... I don't understand why udevadm info -a -p $(udevadm info -q path -n /dev/sda1) https://pastebin.com/Zcz769BQ
If the command shares any resources, and name “autobackup” suggests it does, it should include some mechanism that prevents concurrent execution if another instance is already running. Otherwise, even if a single match happens, multiple instances of the script may be executed. For USB devices that may be a tiny power interruption during insertion, causing it to be detected, removed, detected again: insertion - - - removal - - - insertion - - - - - - - |-- script ---------------------------------------> \_ two instances |-- script ---------> / of the code It should be either of those: |-- script -------------------------------------------------> |--- BAIL OUT! -| or |-- script --------------------------------- end| |-- WAIT .......... script ---> > SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", > ATTRS{idProduct}=="6387", SYMLINK+="external%n", > RUN+="/usr/local/bin/autobackup.sh" > > This works well, buth exec 2 times the script... > > Jun 12 13:53:09 archlaptop autobackup.sh: BEGIN SCRIPT > Jun 12 13:53:13 archlaptop autobackup.sh: BEGIN SCRIPT Because there are two matches for that pattern: - //devices/platform/…/target0:0:0/0:0:0:0/block/sda - //devices/platform/…/target0:0:0/0:0:0:0/block/sda/sda1 Both of them have SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f" and ATTRS{idProduct}=="6387". > If add ENV{DEVTYPE}=="usb_device" and change the > SUBSYSTEM=="usb_device" , when plug usb, udev does nothing ... Because there is nothing that matches SUBSYSTEM=="usb_device".
El sáb, 12 jun 2021 a las 17:43, mpan via arch-general (<arch-general@lists.archlinux.org>) escribió: > > If the command shares any resources, and name “autobackup” suggests > it does, it should include some mechanism that prevents concurrent > execution if another instance is already running. Otherwise, even if a > single match happens, multiple instances of the script may be executed. > For USB devices that may be a tiny power interruption during insertion, > causing it to be detected, removed, detected again: > > insertion - - - removal - - - insertion - - - - - - - > |-- script ---------------------------------------> \_ two instances > |-- script ---------> / of the code > > It should be either of those: > |-- script -------------------------------------------------> > |--- BAIL OUT! -| > or > |-- script --------------------------------- end| > |-- WAIT .......... script ---> > > > SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", > > ATTRS{idProduct}=="6387", SYMLINK+="external%n", > > RUN+="/usr/local/bin/autobackup.sh" > > > > This works well, buth exec 2 times the script... > > > > Jun 12 13:53:09 archlaptop autobackup.sh: BEGIN SCRIPT > > Jun 12 13:53:13 archlaptop autobackup.sh: BEGIN SCRIPT > Because there are two matches for that pattern: > - //devices/platform/…/target0:0:0/0:0:0:0/block/sda > - //devices/platform/…/target0:0:0/0:0:0:0/block/sda/sda1 > Both of them have SUBSYSTEM=="block", ACTION=="add", > ATTRS{idVendor}=="058f" and ATTRS{idProduct}=="6387". > > > If add ENV{DEVTYPE}=="usb_device" and change the > > SUBSYSTEM=="usb_device" , when plug usb, udev does nothing ... > Because there is nothing that matches SUBSYSTEM=="usb_device". > Hi, thanks for your response. I understand, I add the size of partition sda1 for only match pattern --> ATTR{size}=="3930112" SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", ATTRS{idProduct}=="6387", ATTR{size}=="3930112", SYMLINK+="external%n", RUN+="/usr/local/bin/autobackup.sh" And now, only exec the script one time. The name of device change when connect different port usb... I have to put a more precise and less ugly rule. Maybe with KERNEL variable --> KERNEL=="sd*1" : SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", ATTRS{idProduct}=="6387", KERNEL=="sd*1", SYMLINK+="external%n", RUN+="/usr/local/bin/autobackup.sh" With these, works well! Thanks.
On Sat, 12 Jun 2021 at 18:44, Maykel Franco via arch-general < arch-general@lists.archlinux.org> wrote: > El sáb, 12 jun 2021 a las 17:43, mpan via arch-general > (<arch-general@lists.archlinux.org>) escribió: > > > > If the command shares any resources, and name “autobackup” suggests > > it does, it should include some mechanism that prevents concurrent > > execution if another instance is already running. Otherwise, even if a > > single match happens, multiple instances of the script may be executed. > > For USB devices that may be a tiny power interruption during insertion, > > causing it to be detected, removed, detected again: > > > > insertion - - - removal - - - insertion - - - - - - - > > |-- script ---------------------------------------> \_ two instances > > |-- script ---------> / of the code > > > > It should be either of those: > > |-- script -------------------------------------------------> > > |--- BAIL OUT! -| > > or > > |-- script --------------------------------- end| > > |-- WAIT .......... script ---> > > > > > SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", > > > ATTRS{idProduct}=="6387", SYMLINK+="external%n", > > > RUN+="/usr/local/bin/autobackup.sh" > > > > > > This works well, buth exec 2 times the script... > > > > > > Jun 12 13:53:09 archlaptop autobackup.sh: BEGIN SCRIPT > > > Jun 12 13:53:13 archlaptop autobackup.sh: BEGIN SCRIPT > > Because there are two matches for that pattern: > > - //devices/platform/…/target0:0:0/0:0:0:0/block/sda > > - //devices/platform/…/target0:0:0/0:0:0:0/block/sda/sda1 > > Both of them have SUBSYSTEM=="block", ACTION=="add", > > ATTRS{idVendor}=="058f" and ATTRS{idProduct}=="6387". > > > > > If add ENV{DEVTYPE}=="usb_device" and change the > > > SUBSYSTEM=="usb_device" , when plug usb, udev does nothing ... > > Because there is nothing that matches SUBSYSTEM=="usb_device". > > > > Hi, thanks for your response. > > I understand, I add the size of partition sda1 for only match pattern > --> ATTR{size}=="3930112" > > SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", > ATTRS{idProduct}=="6387", ATTR{size}=="3930112", > SYMLINK+="external%n", RUN+="/usr/local/bin/autobackup.sh" > > And now, only exec the script one time. The name of device change when > connect different port usb... > > I have to put a more precise and less ugly rule. Maybe with KERNEL > variable --> KERNEL=="sd*1" : > > SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", > ATTRS{idProduct}=="6387", KERNEL=="sd*1", SYMLINK+="external%n", > RUN+="/usr/local/bin/autobackup.sh" > > With these, works well! > > Thanks. > If you want udev to match on a specific USB stick partition, I would recommend to match on something like the filesystem label (ID_FS_LABEL) or filesystem UUID (ID_FS_UUID). Personally I would go for the filesystem label, because then you can easily create a new USB stick that matches: all you have to do is format the filesystem with the same label. Kind regards, Maarten de Vries
On Sat, 12 Jun 2021 at 13:59, Maykel Franco via arch-general <arch-general@lists.archlinux.org> wrote:
Hi, I try configure the rule in udev for when I connect specific usb device, launch script backup.
I have this rule:
/etc/udev/rules.d/10.autobackup.rules
SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", ATTRS{idProduct}=="6387", SYMLINK+="external%n", RUN+="/usr/local/bin/autobackup.sh"
you'd be better of if you use an oneshot systemd service for this. See https://www.freedesktop.org/software/systemd/man/systemd.device.html#SYSTEMD... RUN in udev rules is not meant for any long-running processes, actually only very short running processes https://www.freedesktop.org/software/systemd/man/udev.html#RUN%7Btype%7D -- damjan
participants (4)
-
Damjan Georgievski
-
Maarten de Vries
-
Maykel Franco
-
mpan