Udev

為什麼我的 udev 規則執行了 3 次

  • November 19, 2021

我制定了一個小 udev 規則,該規則啟動一個腳本,將我的~/Documents目錄存檔。這可以完成工作,但是當我讀取日誌文件時,似乎在我插入密鑰時存檔腳本被執行了 3 次。

這是我的規則:

ACTION=="add", SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{idVendor}=="0951", ATTRS{idProduct}=="1642", RUN+="/bin/sh /root/auto-archive" 

這是我的腳本:

#! /bin/sh

sleep 5

# test if awesome is running
if ps aux | grep -v launch | grep -v grep | grep awesome
then
   echo awesome is running >> /home/purplepsycho/log
else
   # echo awesome not running another guy must be logged
   exit
fi

if mount | grep /media/usb
then
   echo /media/usb already mounted >> /home/purplepsycho/log
   exit
fi

# mount key
echo mounting... >> /home/purplepsycho/log
/usr/bin/sudo -u purplepsycho mount /media/usb

# test if mount have been succesful
if [ $? -ne 0 ]
then
   echo mount failed >> /home/purplepsycho/log
   exit
fi
# archive dir
ARC_DIR="/media/usb/archive"

# make directory
mkdir -p $ARC_DIR

# archive name
NAME=$(date +"archive-%Y-%m-%d.tgz")

# test if an archive already exists for today
if [ -f $ARC_DIR/$NAME ] 
then
   echo archive file already exists for today >> /home/purplepsycho/log
   exit
fi

# initialize log file
echo $NAME > $ARC_DIR/files.txt

# make the archive
tar -zcvf $ARC_DIR/$NAME /home/purplepsycho/Documents/* >> $ARC_DIR/files.txt

任何的想法?謝謝。

—在 Sparhawk 評論後編輯—

udevadm在我的鑰匙上執行:

udevadm info -a -p $(udevadm info -q path -n /dev/sbd)

這給了:

looking at device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb':
 KERNEL=="sdb"
 SUBSYSTEM=="block"
 DRIVER==""

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0':
 KERNELS=="4:0:0:0"
 SUBSYSTEMS=="scsi"
 DRIVERS=="sd"

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4/target4:0:0':
 KERNELS=="target4:0:0"
 SUBSYSTEMS=="scsi"
 DRIVERS==""

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host4':
 KERNELS=="host4"
 SUBSYSTEMS=="scsi"
 DRIVERS==""

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0':
 KERNELS=="1-1:1.0"
 SUBSYSTEMS=="usb"
 DRIVERS=="usb-storage"

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-1':
 KERNELS=="1-1"
 SUBSYSTEMS=="usb"
 DRIVERS=="usb"
 ATTRS{idVendor}=="0951"
 ATTRS{idProduct}=="1642"

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1':
 KERNELS=="usb1"
 SUBSYSTEMS=="usb"
 DRIVERS=="usb"
 ATTRS{idVendor}=="1d6b"
 ATTRS{idProduct}=="0002"

looking at parent device '/devices/pci0000:00/0000:00:1d.7':
 KERNELS=="0000:00:1d.7"
 SUBSYSTEMS=="pci"
 DRIVERS=="ehci-pci"
 ATTRS{irq}=="23"

looking at parent device '/devices/pci0000:00':
 KERNELS=="pci0000:00"
 SUBSYSTEMS==""
 DRIVERS==""

我試圖僅基於以下內容編寫規則:

 KERNELS=="1-1"
 SUBSYSTEMS=="usb"
 DRIVERS=="usb"
 ATTRS{idVendor}=="0951"
 ATTRS{idProduct}=="1642"

 KERNELS=="usb1"
 SUBSYSTEMS=="usb"
 DRIVERS=="usb"
 ATTRS{idVendor}=="1d6b"
 ATTRS{idProduct}=="0002"

結果是一樣的:腳本被多次呼叫…

問題來自您的規則:您只嘗試匹配設備的父級,而不是設備本身…有關詳細資訊,請參閱http://reactivated.net/writing_udev_rules.html#sysfstree

  • 您必須匹配設備(KERNEL=, SUBSYSTEM=, ATTR=…),
  • 及其父項之一(KERNELS=, SUBSYSTEMS=, ATTRS=…)(請注意欄位末尾的**“S” )。**

修正後的規則是:

ACTION=="add", KERNEL=="sdb1", SUBSYSTEM=="block", ATTRS{vendor}=="Kingston", ATTRS{model}=="DT 101 G2", RUN+="/bin/sh /root/auto-archive"

我只是在這裡添加我的(經驗)發現:以防它幫助其他人。

我正在編寫一個腳本,以便在將Clip Jam插入 Raspberry Pi 4 的 USB 埠時執行。(執行 Rasbian Buster,2021 年末發布):這本質上是一個 USB 儲存設備 - 並作為文件系統安裝。

/dev/sda1       7.8G  7.6G  206M  98% /media/pi/Clip Jam

當我插入設備時,我還看到我的腳本執行了多次(比如 25 次!)。

上面的文章確實有助於注意到命令輸出中單數(“核心”)和復數條目(“核心”)之間的區別:

udevadm info -a /dev/sda1

但我不欣賞的是規則似乎匹配的內容。您似乎需要匹配在父列表中僅出現一次的資訊。

就我而言;我能夠辨識出唯一具有此條目的父項:

ATTRS{product}=="Clip Jam"

所以我寫的對我有用的規則(/etc/udev/rules.d/999-tester.rules)是這樣的:

ACTION=="add" \
, KERNEL=="sda1" \
, SUBSYSTEM=="block" \
, ATTRS{product}=="Clip Jam" \
, ENV{DISPLAY}=":0" \
, ENV{XAUTHORITY}="/home/pi/.Xauthority" \
, RUN+="/home/pi/Desktop/tester.sh"

這裡有註釋:

ACTION=="add" \ <-- The action we are interested in.
, KERNEL=="sda1" \ <---- Top of the output of the 'udevadm' command.
, SUBSYSTEM=="block" \ <---- Top of the output of the 'udevadm' command.
, ATTRS{product}=="Clip Jam" \ <--- Found (trial and error) in list of parents; unique entry throughout 'udevadm' output.
, ENV{DISPLAY}=":0" \ <-- Taken from 'env' output
, ENV{XAUTHORITY}="/home/pi/.Xauthority" \ <-- Taken from 'env' output
, RUN+="/home/pi/Desktop/tester.sh" <-- Testing script only.

另一個要記住:編輯規則命令後重新執行刷新命令!(容易忘記!)

sudo udevadm control --reload

引用自:https://unix.stackexchange.com/questions/249565