我制定了一些 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"
结果是相同的:脚本被调用多次......
答案1
问题来自您的规则:您只尝试匹配设备的父设备,而不是设备本身......请参阅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"
答案2
我只是在这里添加我的(经验)发现:以防对其他人有帮助。
我正在编写一个脚本,当剪辑果酱插入 Raspberry Pi 4 的 USB 端口。(运行 Rasbian Buster,2021 年末发布):这本质上是一个 USB 存储设备 - 并作为文件系统安装。
/dev/sda1 7.8G 7.6G 206M 98% /media/pi/Clip Jam
当我插入设备时,我还看到我的脚本运行了多次(比如 25 次!)。
上面的帖子确实有助于注意到命令输出中单数(“Kernel”)和复数条目(“Kernels”)之间的区别:
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