![udev 规则自动执行脚本](https://linux22.com/image/1451791/udev%20%E8%A7%84%E5%88%99%E8%87%AA%E5%8A%A8%E6%89%A7%E8%A1%8C%E8%84%9A%E6%9C%AC.png)
我想创建一个 udev 规则,在耳机插入时执行。因此我遵循这教程,但我的规则似乎不起作用。
输出
udevadm monitor --environment | fgrep 'DEVNAME=/dev'
插入耳机后是:
DEVNAME=/dev/bus/usb/001/023
DEVNAME=/dev/snd/controlC2
DEVNAME=/dev/snd/pcmC2D0p
DEVNAME=/dev/snd/pcmC2D0c
DEVNAME=/dev/input/event19
DEVNAME=/dev/hidraw4
DEVNAME=/dev/bus/usb/001/023
DEVNAME=/dev/snd/pcmC2D0p
DEVNAME=/dev/hidraw4
DEVNAME=/dev/snd/pcmC2D0c
DEVNAME=/dev/input/event19
DEVNAME=/dev/snd/controlC2
我选择了/dev/hidraw4,当我输入
udevadm info --attribute-walk --name /dev/hidraw4
我得到以下输出:
looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.3/0003:046D:0A0B.0018/hidraw/hidraw4':
*KERNEL=="hidraw4"
*SUBSYSTEM=="hidraw"
DRIVER==""
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.3/0003:046D:0A0B.0018':
KERNELS=="0003:046D:0A0B.0018"
SUBSYSTEMS=="hid"
DRIVERS=="hid-generic"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.3':
KERNELS=="1-1:1.3"
SUBSYSTEMS=="usb"
DRIVERS=="usbhid"
ATTRS{bInterfaceClass}=="03"
ATTRS{bInterfaceSubClass}=="00"
ATTRS{bInterfaceProtocol}=="00"
ATTRS{bNumEndpoints}=="01"
ATTRS{supports_autosuspend}=="1"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bInterfaceNumber}=="03"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-1':
KERNELS=="1-1"
*SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="00"
ATTRS{devpath}=="1"
ATTRS{idVendor}=="046d"
ATTRS{speed}=="12"
ATTRS{bNumInterfaces}==" 4"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="8"
ATTRS{busnum}=="1"
ATTRS{devnum}=="24"
ATTRS{configuration}=="G8 v3.0.0.0"
ATTRS{bMaxPower}=="100mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="80"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="0"
ATTRS{bcdDevice}=="1013"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="661213"
ATTRS{ltm_capable}=="no"
*ATTRS{manufacturer}=="Logitech"
ATTRS{removable}=="unknown"
ATTRS{idProduct}=="0a0b"
ATTRS{bDeviceClass}=="00"
*ATTRS{product}=="Logitech USB Headset"
looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1':
KERNELS=="usb1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="01"
ATTRS{devpath}=="0"
ATTRS{idVendor}=="1d6b"
ATTRS{speed}=="480"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{authorized_default}=="1"
ATTRS{busnum}=="1"
ATTRS{devnum}=="1"
ATTRS{configuration}==""
ATTRS{bMaxPower}=="0mA"
ATTRS{authorized}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bNumConfigurations}=="1"
ATTRS{maxchild}=="9"
ATTRS{bcdDevice}=="0400"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{quirks}=="0x0"
ATTRS{serial}=="0000:00:14.0"
ATTRS{version}==" 2.00"
ATTRS{urbnum}=="472"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="Linux 4.0.0-040000-generic xhci-hcd"
ATTRS{removable}=="unknown"
ATTRS{idProduct}=="0002"
ATTRS{bDeviceClass}=="09"
ATTRS{product}=="xHCI Host Controller"
looking at parent device '/devices/pci0000:00/0000:00:14.0':
KERNELS=="0000:00:14.0"
SUBSYSTEMS=="pci"
DRIVERS=="xhci_hcd"
ATTRS{irq}=="40"
ATTRS{subsystem_vendor}=="0x17aa"
ATTRS{broken_parity_status}=="0"
ATTRS{class}=="0x0c0330"
ATTRS{driver_override}=="(null)"
ATTRS{consistent_dma_mask_bits}=="64"
ATTRS{dma_mask_bits}=="64"
ATTRS{local_cpus}=="0f"
ATTRS{device}=="0x9c31"
ATTRS{enable}=="1"
ATTRS{msi_bus}=="1"
ATTRS{local_cpulist}=="0-3"
ATTRS{vendor}=="0x8086"
ATTRS{subsystem_device}=="0x220c"
ATTRS{numa_node}=="-1"
ATTRS{d3cold_allowed}=="1"
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
*
我为我的 udev 规则选择了带星号的值。
我的完整规则是:
KERNEL=="hidraw4", SUBSYSTEM=="hidraw", SUBSYSTEMS=="usb", ATTRS{manufacturer}=="Logitech", ATTRS{product}=="Logitech USB Headset", ACTION=="add", RUN+="/usr/local/bin/run-script-in-background.sh /usr/local/bin/usb-headset-set-default.sh"
如果输入到终端中,这两个脚本都可以工作,因此错误一定出在 udev 规则的某个地方!