每当我将键盘插入Raspberry Pi时,我都会尝试运行一些代码(最好是在脚本中)。我目前安装了Rasbian Jessie(可能是最新版本?)。我通过研究发现我应该使用" udev"当usb设备插入Linux机器时触发事件。
为了测试这个,我希望每当我插入键盘时打开浏览器(它试图运行的命令在我的显示器插入的树莓派上的终端中工作)。我已创建以下文件/etc/udev/rules.d/keyboard.rules
:
ACTION=="add",ATTRS{idVendor}=="413c",RUN+="epiphany --display=:0 http://www.google.com"
我从lsusb
命令获得了idVendor:Bus 001 Device 027: ID 413c:2105 Dell Computer Corp. Model L100 Keyboard
但当我拔掉电源插头时插回我的键盘没有任何反应。
我还尝试了什么:
sudo udevadm control --reload-rules
sudo udevadm control --reload
RUN+='/home/pi/test.sh'
(已设置为可运行)。udevadm info -a -p $(udevadm info -q path -n /dev/input/event0)
验证idVendor(它为键盘提供了所有ATTRS)udevadm monitor
& udevadm monitor --property
因为我正在插入键盘udevadm test $(udevadm info -q path -n /dev/input/event0)
对其进行测试。这将显示以下信息:calling: test version 215 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. load module index Network interface NamePolicy= disabled on kernel commandline, ignoring. 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 timestamp of '/lib/udev/rules.d' changed read rules file: /lib/udev/rules.d/10-local-rpi.rules read rules file: /etc/udev/rules.d/40-scratch.rules read rules file: /lib/udev/rules.d/42-usb-hid-pm.rules read rules file: /lib/udev/rules.d/50-bluetooth-hci-auto-poweron.rules read rules file: /lib/udev/rules.d/50-firmware.rules read rules file: /lib/udev/rules.d/50-udev-default.rules read rules file: /lib/udev/rules.d/55-dm.rules read rules file: /lib/udev/rules.d/60-cdrom_id.rules read rules file: /lib/udev/rules.d/60-crda.rules read rules file: /lib/udev/rules.d/60-drm.rules read rules file: /lib/udev/rules.d/60-fuse.rules read rules file: /lib/udev/rules.d/60-gnupg.rules read rules file: /lib/udev/rules.d/60-keyboard.rules read rules file: /lib/udev/rules.d/60-libgphoto2-6.rules read rules file: /lib/udev/rules.d/60-libpisock9.rules read rules file: /lib/udev/rules.d/60-persistent-alsa.rules read rules file: /lib/udev/rules.d/60-persistent-input.rules read rules file: /lib/udev/rules.d/60-persistent-serial.rules read rules file: /lib/udev/rules.d/60-persistent-storage-dm.rules read rules file: /lib/udev/rules.d/60-persistent-storage-tape.rules read rules file: /lib/udev/rules.d/60-persistent-storage.rules read rules file: /lib/udev/rules.d/60-persistent-v4l.rules read rules file: /lib/udev/rules.d/60-triggerhappy.rules read rules file: /lib/udev/rules.d/61-accelerometer.rules read rules file: /lib/udev/rules.d/64-btrfs.rules read rules file: /lib/udev/rules.d/64-xorg-xkb.rules read rules file: /lib/udev/rules.d/69-libmtp.rules read rules file: /lib/udev/rules.d/70-power-switch.rules read rules file: /lib/udev/rules.d/70-uaccess.rules read rules file: /lib/udev/rules.d/71-seat.rules read rules file: /lib/udev/rules.d/73-idrac.rules read rules file: /lib/udev/rules.d/73-seat-late.rules read rules file: /lib/udev/rules.d/75-net-description.rules read rules file: /lib/udev/rules.d/75-persistent-net-generator.rules read rules file: /lib/udev/rules.d/75-probe_mtd.rules read rules file: /lib/udev/rules.d/75-tty-description.rules read rules file: /lib/udev/rules.d/78-sound-card.rules read rules file: /lib/udev/rules.d/80-drivers.rules read rules file: /lib/udev/rules.d/80-net-setup-link.rules read rules file: /lib/udev/rules.d/80-networking.rules read rules file: /lib/udev/rules.d/80-udisks.rules read rules file: /lib/udev/rules.d/80-udisks2.rules read rules file: /lib/udev/rules.d/85-hdparm.rules read rules file: /lib/udev/rules.d/85-hwclock.rules read rules file: /lib/udev/rules.d/85-regulatory.rules read rules file: /lib/udev/rules.d/90-alsa-restore.rules read rules file: /lib/udev/rules.d/95-udev-late.rules read rules file: /lib/udev/rules.d/95-wedo.rules read rules file: /lib/udev/rules.d/97-hid2hci.rules read rules file: /etc/udev/rules.d/99-com.rules read rules file: /lib/udev/rules.d/99-systemd.rules read rules file: /etc/udev/rules.d/keyboard.rules read rules file: /etc/udev/rules.d/test.rules rules contain 393216 bytes tokens (32768 * 12 bytes), 23387 bytes strings 23799 strings (190642 bytes), 21572 de-duplicated (169483 bytes), 2228 trie nodes used IMPORT builtin 'input_id' /lib/udev/rules.d/50-udev-default.rules:10 capabilities/ev raw kernel attribute: 120013 capabilities/abs raw kernel attribute: 0 capabilities/rel raw kernel attribute: 0 capabilities/key raw kernel attribute: 10000 7 ff9f207a c14057ff febeffdf ffefffff ffffffff fffffffe test_key: checking bit block 0 for any keys; found=1 test_key: checking bit block 32 for any keys; found=1 test_key: checking bit block 64 for any keys; found=1 test_key: checking bit block 96 for any keys; found=1 test_key: checking bit block 128 for any keys; found=1 test_key: checking bit block 160 for any keys; found=1 test_key: checking bit block 192 for any keys; found=1 test_key: checking bit block 224 for any keys; found=1 GROUP 101 /lib/udev/rules.d/50-udev-default.rules:29 IMPORT builtin 'hwdb' /lib/udev/rules.d/60-keyboard.rules:12 IMPORT builtin 'hwdb' returned non-zero IMPORT builtin 'hwdb' /lib/udev/rules.d/60-keyboard.rules:20 IMPORT builtin 'hwdb' returned non-zero IMPORT builtin 'usb_id' /lib/udev/rules.d/60-persistent-input.rules:7 /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0: if_class 3 protocol 0 LINK 'input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd' /lib/udev/rules.d/60-persistent-input.rules:24 IMPORT builtin 'path_id' /lib/udev/rules.d/60-persistent-input.rules:31 LINK 'input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd' /lib/udev/rules.d/60-persistent-input.rules:33 RUN '/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev' /lib/udev/rules.d/60-triggerhappy.rules:4 GROUP 101 /etc/udev/rules.d/99-com.rules:1 MODE 0660 /etc/udev/rules.d/99-com.rules:1 RUN 'epiphany --display=:0 http://www.google.com' /etc/udev/rules.d/keyboard.rules:1 handling device node '/dev/input/event0', devnum=c13:64, mode=0660, uid=0, gid=101 preserve permissions /dev/input/event0, 020660, uid=0, gid=101 preserve already existing symlink '/dev/char/13:64' to '../input/event0' found 'c13:64' claiming '/run/udev/links/\x2finput\x2fby-id\x2fusb-Dell_Dell_USB_Keyboard-event-kbd' creating link '/dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd' to '/dev/input/event0' preserve already existing symlink '/dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd' to '../event0' found 'c13:64' claiming '/run/udev/links/\x2finput\x2fby-path\x2fplatform-3f980000.usb-usb-0:1.2:1.0-event-kbd' creating link '/dev/input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd' to '/dev/input/event0' preserve already existing symlink '/dev/input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd' to '../event0' unable to create temporary db file '/run/udev/data/c13:64.tmp': Permission denied .INPUT_CLASS=kbd ACTION=add BACKSPACE=guess DEVLINKS=/dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd /dev/input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd DEVNAME=/dev/input/event0 DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/0003:413C:2105.0014/input/input23/event0 ID_BUS=usb ID_INPUT=1 ID_INPUT_KEY=1 ID_INPUT_KEYBOARD=1 ID_MODEL=Dell_USB_Keyboard ID_MODEL_ENC=Dell\x20USB\x20Keyboard ID_MODEL_ID=2105 ID_PATH=platform-3f980000.usb-usb-0:1.2:1.0 ID_PATH_TAG=platform-3f980000_usb-usb-0_1_2_1_0 ID_REVISION=0352 ID_SERIAL=Dell_Dell_USB_Keyboard ID_TYPE=hid ID_USB_DRIVER=usbhid ID_USB_INTERFACES=:030101: ID_USB_INTERFACE_NUM=00 ID_VENDOR=Dell ID_VENDOR_ENC=Dell ID_VENDOR_ID=413c MAJOR=13 MINOR=64 SUBSYSTEM=input USEC_INITIALIZED=78790607 XKBLAYOUT=us XKBMODEL=pc105 XKBOPTIONS=terminate:ctrl_alt_bksp XKBVARIANT= run: '/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev' run: 'epiphany --display=:0 http://www.google.com' unload module index Unloaded link configuration context.
如果向下滚动到底部,则会看到run: 'epiphany --display=:0 http://www.google.com'
。这让我相信它应该有用。
但当我拔掉电源插头时把我的键盘插回去没有任何反应....
我还没有能够udevadm trigger
做任何事情(尝试了多条路径和其他输入,但它什么也没有返回)。
是否有某种日志我可以检查它是否尝试运行我的命令/脚本?有没有其他方法来监控此脚本?有没有其他方法来测试这个脚本,所以我实际上可以看到它运行而没有实际插入USB设备?
答案 0 :(得分:1)
是否有某种日志我可以检查它是否尝试运行我的命令/脚本?还有另一种监控此脚本的方法吗?
找到this线程后,我发现您可以在/var/log/syslog
文件中找到系统日志文件中的udev日志。这将默认仅显示错误,但如果您将日志级别提升到" debug"你能够看到一切。我首先运行udevadm control --log-priority=debug
并将/etc/udev/udev.conf
文件更新为" debug"。
我发现当我插入键盘时,系统会抛出以下错误:
failed to execute '/lib/udev/epiphany --display=:0 http://www.google.com': No such file or directory
。
啊哈似乎是在试图从/ lib / udev文件夹中运行顿悟。所以我再次更新代码以运行正确的文件路径到顿::/usr/bin/epiphany --display=:0 http://www.google.com
。但是我得到了以下错误:
Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com'(err) 'No protocol specified'
Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com'(err) 'Unable to init server: Could not connect: Connection refused'
Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com'(out) 'Failed to parse arguments: Cannot open display: :0'
Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com' [6479] exit with return code 1
如果我将它直接粘贴到终端中,这段代码可以正常工作,因此奇怪的是它无法找到显示...最终我发现udev将所有内容都运行为" root"。显然" root"无法访问显示器。当我通过终端运行命令时,它使用用户" pi"它可以访问显示器。所以命令的最终编辑:/sbin/runuser -l pi -c '/usr/bin/epiphany --display=:0 http://www.google.com'
。它工作!
当前的小打嗝是打开2个浏览器,但我认为这仅仅是因为我只是在检查" idVendor"并且usb设备有很多孩子可以分享这个" idVendor"。因此,当插入单个键盘时,它会看到多个USB设备并多次运行udev规则。
有没有其他方法来测试这个脚本,所以我实际上可以看到它运行 没有插入USB设备?
我在上面的评论中提到过这一点,但我发现你可以运行以下命令来触发udev命令,而不必解除插件插回键盘:sudo udevadm trigger -v -c add -a idVendor=413c