![当界面出现时运行的脚本(不一定会出现)](https://linux22.com/image/152919/%E5%BD%93%E7%95%8C%E9%9D%A2%E5%87%BA%E7%8E%B0%E6%97%B6%E8%BF%90%E8%A1%8C%E7%9A%84%E8%84%9A%E6%9C%AC%EF%BC%88%E4%B8%8D%E4%B8%80%E5%AE%9A%E4%BC%9A%E5%87%BA%E7%8E%B0%EF%BC%89.png)
我有一个 USB 网络接口,当我插入 USB 密钥时会出现该接口,或者有时,当我启动树莓派时 USB 密钥已经插入。此问题的通常解决方案是添加 udev 脚本,如下所示:
/udev/rules.d/50-stick.rules
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="19d2", ATTRS{idProduct}=="1405", RUN+="/usr/bin/netctl restart usb0"
但这不起作用,无论是在启动时还是在我(重新)插入棒时。是否存在另一种方式?我想绕过 udev 方法并在出现接口(例如 usb0)时启动脚本。
编辑:为什么这不起作用:
ACTION=="add", SUBSYSTEM=="net", ENV{INTERFACE}=="usb0", RUN+="/usr/bin/netctl restart usb0"
一些信息:
udevadm info /sys/class/net/usb0
P: /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/net/usb0
L: 0
E: DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/net/usb0
E: INTERFACE=usb0
E: IFINDEX=5
E: SUBSYSTEM=net
E: USEC_INITIALIZED=508278395
E: ID_NET_NAMING_SCHEME=v240
E: ID_VENDOR=ZTE_Incorporated
E: ID_VENDOR_ENC=ZTE\x2cIncorporated
E: ID_VENDOR_ID=19d2
E: ID_MODEL=ZTE_Mobile_Broadband
E: ID_MODEL_ENC=ZTE\x20Mobile\x20Broadband
E: ID_MODEL_ID=1405
E: ID_REVISION=5486
E: ID_SERIAL=ZTE_Incorporated_ZTE_Mobile_Broadband_1234567890ABCDEF
E: ID_SERIAL_SHORT=1234567890ABCDEF
E: ID_TYPE=generic
E: ID_BUS=usb
E: ID_USB_INTERFACES=:020600:0a0000:080650:
E: ID_USB_INTERFACE_NUM=00
E: ID_USB_DRIVER=cdc_ether
E: ID_USB_CLASS_FROM_DATABASE=Communications
E: ID_VENDOR_FROM_DATABASE=ZTE WCDMA Technologies MSM
E: ID_PATH=platform-3f980000.usb-usb-0:1.3:1.0
E: ID_PATH_TAG=platform-3f980000_usb-usb-0_1_3_1_0
E: ID_NET_DRIVER=cdc_ether
E: ID_NET_LINK_FILE=/usr/lib/systemd/network/99-default.link
E: SYSTEMD_ALIAS=/sys/subsystem/net/devices/usb0
E: TAGS=:systemd:
答案1
最后,我采用了这个解决方案(/etc/udev/rules.d/99-stick.rules
):
ACTION=="add", SUBSYSTEM=="net", ENV{INTERFACE}=="usb0", RUN+="/usr/bin/netctl restart usb0", RUN+="/usr/bin/systemctl restart ntpdate"