udev规则有问题

udev规则有问题

当我插入蓝牙键盘时,我在运行 .sh 脚本时遇到困难。

我正在使用以下 udev 规则:

ACTION=="add", SUBSYSTEM=="bluetooth", RUN+="./scripts/icleverkeybindings.sh" 

当我连接键盘时,该脚本似乎没有被激活。我还尝试过“./scripts/iceverkeybindings.sh”、“/bin/bash /scripts/iceverkeybindings.sh”和“sh /scripts/iceverkeybindings.sh”。我不明白为什么这个脚本没有被激活。

如果我改为使用以下 udev 规则

ACTION=="add", SUBSYSTEM=="bluetooth", RUN+="/bin/mkdir /home/joe/tempfolder" 

然后,当我连接键盘时,会创建文件夹 /home/joe/tempfolder 。

我已将规则保存在 /etc/udev/rules.d/50-addiceverkeybindings.sh 中。权限等为:-rw-r--r-- 1 joe root 80 Aug 11 19:53 50-addiceverkeyboard.rules。

我尝试运行的脚本如下:

#!/bin/bash

sleep 1
cheese

remote_id=$(
    xinput list |
    sed -n 's/.*iClever IC-BK06 Keyboard  .*id=\([0-9]*\).*keyboard.*/\1/p'
)
[ "$remote_id" ] || exit

xkbcomp -i $remote_id /scripts/icleverlayout.xkb $DISPLAY

ls -la 给出 -rwxr-xr-x 1 joe root 222 Aug 11 20:00 iceverkeybindings.sh。

如果我从终端简单地调用 /scripts/iceverkeybindings.sh ,那么效果很好。

该脚本更改了我的键盘的键绑定,我添加了暂停,以防在 xinput 知道键盘在那里之前脚本被调用,并且我添加了奶酪只是为了确保我知道它是否肯定被调用或不是。

谁能告诉我如何更改规则以便调用脚本?

谢谢

编辑:为了澄清,我创建了一个文件夹 /scripts/,所以 /scripts/iceverkeybindings.sh 是绝对文件路径

答案1

正如它所说man udev

   RUN{type}
       Add a program to the list of programs to be executed after
       processing all the rules for a specific event, depending on
       "type":

       "program"
           Execute an external program specified as the assigned
           value. If no absolute path is given, the program is
           expected to live in /lib/udev; otherwise, the absolute
           path must be specified.

您还可以检查哪些udev日志:

Aug 11 21:57:43 ja-VirtualBox systemd-udevd[2998]: failed to execute '/lib/udev/./scripts/icleverkeybindings.sh' './scripts/icleverkeybindings.sh': No such file or directory

或者非 systemd 系统:

[769712.027218] udevd[13015]: failed to execute '/lib/udev/./scripts/icleverkeybindings.sh' './scripts/icleverkeybindings.sh': No such file or directory

话虽如此,您必须 icleverkeybindings.sh在 udev 规则中传递绝对路径,如下所示:

ACTION=="add", SUBSYSTEM=="bluetooth", RUN+="/path/to/icleverkeybindings.sh"

或者在 udev 规则中icleverkeybindings.sh输入/lib/udev并使用文件名:

ACTION=="add", SUBSYSTEM=="bluetooth", RUN+="icleverkeybindings.sh"

在这两种情况下,您都需要确保icleverkeybindings.sh 设置了可执行位,否则:

Aug 11 22:07:49 ja-VirtualBox systemd-udevd[3345]: failed to execute '/lib/udev/icleverkeybindings.sh' 'icleverkeybindings.sh': Permission denied
Aug 11 22:07:49 ja-VirtualBox systemd-udevd[3325]: Process 'icleverkeybindings.sh' failed with exit code 2.

相关内容