Por que meu script disparado pelo udev não está funcionando?

2

Eu criei uma regra do udev para sincronizar minha unidade USB com minha pasta de documentos, mas parece que não está funcionando.

95-usb-auto-sync.rules :

ACTION=="add", ATTRS{idVendor}=="0930", ATTRS{idProduct}=="1408", RUN+="/home/riccardo/Documents/Scripts/sync-riccardo-usb.sh"

/home/riccardo/Documents/Scripts/sync-riccardo-usb.sh :

#!/bin/bash

rsync -a --delete '/home/riccardo/Documents/Development/' '/run/media/riccardo/Riccardo/Documents/Development' --exclude='*/node_modules' >> /home/riccardo/Documents/Scripts/log.txt
rsync -a --delete '/home/riccardo/Documents/KiCAD/' '/run/media/riccardo/Riccardo/Documents/KiCAD' >> /home/riccardo/Documents/Scripts/log.txt
rsync -a --delete '/home/riccardo/Documents/Personal Files/' '/run/media/riccardo/Riccardo/Documents/Personal Files' >> /home/riccardo/Documents/Scripts/log.txt
rsync -a --delete '/home/riccardo/Documents/Scripts/' '/run/media/riccardo/Riccardo/Documents/Scripts' >> /home/riccardo/Documents/Scripts/log.txt

Eu adicionei o '> > /home/riccardo/Documents/Scripts/log.txt 'part para verificar se o script foi realmente executado. O arquivo log.txt permaneceu em branco.

Informações sobre o Udev da unidade USB da Toshiba :

  looking at device '/devices/pci0000:00/0000:00:14.0/usb4/4-2':
    KERNEL=="4-2"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{authorized}=="1"
    ATTR{avoid_reset_quirk}=="0"
    ATTR{bConfigurationValue}=="1"
    ATTR{bDeviceClass}=="00"
    ATTR{bDeviceProtocol}=="00"
    ATTR{bDeviceSubClass}=="00"
    ATTR{bMaxPacketSize0}=="9"
    ATTR{bMaxPower}=="296mA"
    ATTR{bNumConfigurations}=="1"
    ATTR{bNumInterfaces}==" 1"
    ATTR{bcdDevice}=="0001"
    ATTR{bmAttributes}=="80"
    ATTR{busnum}=="4"
    ATTR{configuration}==""
    ATTR{devnum}=="2"
    ATTR{devpath}=="2"
    ATTR{idProduct}=="1408"
    ATTR{idVendor}=="0930"
    ATTR{ltm_capable}=="no"
    ATTR{manufacturer}=="TOSHIBA"
    ATTR{maxchild}=="0"
    ATTR{product}=="USB FLASH DRIVE"
    ATTR{quirks}=="0x0"
    ATTR{removable}=="removable"
    ATTR{serial}=="C03FD5FBA9CDC1401D7FB227"
    ATTR{speed}=="5000"
    ATTR{urbnum}=="11182"
    ATTR{version}==" 3.00"

Obrigado,

Riccardo

    
por Kopharex 13.07.2017 / 07:01

2 respostas

2

man udev explica que a chave RUN+= não deve ser usada para tarefas de longa execução:

This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device.

Starting daemons or other long-running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished.

A abordagem correta, para sistemas modernos que usam o systemd, é SYSTEMD_WANTS . De man systemd.device :

THE UDEV DATABASE

The settings of device units may either be configured via unit files, or directly from the udev database (which is recommended). The following udev device properties are understood by systemd:


SYSTEMD_WANTS=, SYSTEMD_USER_WANTS= Adds dependencies of type Wants from the device unit to all listed units. The first form is used by the system systemd instance, the second by user systemd instances. Those settings may be used to activate arbitrary units when a specific device becomes available.

Note that this and the other tags are not taken into account unless the device is tagged with the "systemd" string in the udev database, because otherwise the device is not exposed as a systemd unit (see above).

Assim, sua regra do udev se tornaria:

ACTION=="add", ATTRS{idVendor}=="0930", ATTRS{idProduct}=="1408", SYSTEMD_WANTS="rsync_backup.service"

E você escreve um arquivo de serviço para chamar seu script de backup quando o dispositivo é montado:

[Unit]
Description=Backup riccardo's stuff
Requires=media-Riccardo.mount
After=media-Riccardo.mount

[Service]
ExecStart=/usr/local/bin/rsync_backup_thing

[Install]
WantedBy=media-Riccardo.mount

Quando /media/Riccardo for montado, seu script bash será acionado.

    
por 13.07.2017 / 07:27
1

Como @jasonwryan sugeriu que eu alterasse a regra do udev para:

ACTION=="add", ATTRS{idVendor}=="0930", ATTRS{idProduct}=="1408", ENV{SYSTEMD_WANTS}="usb-auto-sync.service"

com usb-auto-sync.service:

[Unit]
Description=Auto Sync USB

[Service]
ExecStart=/home/riccardo/Documents/Scripts/sync-riccardo-usb.sh
StandardOutput=null

[Install]
WantedBy=multi-user.target
Alias=usbauto-sync.service

Eu ativei o serviço e agora o script está sendo disparado.

    
por 13.07.2017 / 07:23

Tags