无法从 udev 规则执行脚本

无法从 udev 规则执行脚本

我正在尝试在 Ubuntu 17.10 上创建 udev 规则。经过几个小时的测试,我最终得到了以下规则/etc/udev/rules.d/98-intelpwm.rules

(仅用于测试目的!)

ACTION=="add", RUN+="/home/user/intel_gpu/test.sh"

脚本本身:

$ ll /home/user/intel_gpu/test.sh
-rwxrwxrwx 1 user user 56 Oct 22 01:38 /home/user/intel_gpu/test.sh*

#!/bin/sh
/bin/echo 1 >> /home/user/intel_gpu/test.output

当我运行时,udevadm test /module/usb_storage我可以看到规则触发但test.output尚未创建(或者如果我手动创建它,它不会更新)。

calling: test
version 234
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

=== trie on-disk ===
tool version:          234
file size:         9138024 bytes
header size             80 bytes
strings            1910832 bytes
nodes              7227112 bytes
Load module index
Failed to read $container of PID 1, ignoring: Permission denied
Found container virtualization none.
timestamp of '/etc/systemd/network' changed
timestamp of '/lib/systemd/network' changed
Parsed configuration file /lib/systemd/network/99-default.link
Created link configuration context.
timestamp of '/etc/udev/rules.d' changed
Reading rules file: /lib/udev/rules.d/39-usbmuxd.rules
Reading rules file: /lib/udev/rules.d/40-usb-media-players.rules
...
Reading rules file: /etc/udev/rules.d/98-intelpwm.rules
Reading rules file: /lib/udev/rules.d/99-systemd.rules
rules contain 393216 bytes tokens (32768 * 12 bytes), 35397 bytes strings
27571 strings (233697 bytes), 24356 de-duplicated (201516 bytes), 3216 trie nodes used
value '[dmi/id]sys_vendor' is 'ASUSTeK COMPUTER INC.'
value '[dmi/id]sys_vendor' is 'ASUSTeK COMPUTER INC.'
RUN '/home/user/intel_gpu/test.sh' /etc/udev/rules.d/98-intelpwm.rules:1
ACTION=add
DEVPATH=/module/usb_storage
SUBSYSTEM=module
USEC_INITIALIZED=4988363806
run: '/home/user/intel_gpu/test.sh'
Unload module index
Unloaded link configuration context.

我错过了什么?

答案1

要应用udev规则,您必须使用udevadm trigger。此外,您还可以指定udevadm trigger要触发哪个动作事件。

如果您只是运行,udevadm trigger所有设备都会被触发,并且会有一些add操作触发您的脚本,因为它对所有设备都有效。
如果您想udev为已经可用的特定设备运行规则,您可能需要添加该-c, --action=ACTION选项。

udevadm trigger --action=add /sys/<path-to-your-device>

我强烈建议限制udev规则的匹配,因为目前它确实匹配任何设备并且您的脚本将运行多次。

相关内容