Ubuntu Server 21.10 - 如何在插入 USB 设备时运行 Shell 脚本

问题描述 投票:0回答:2

我想在插入 USB 设备时运行特定的脚本。但是,当我使用 USB 设备时,什么也没有发生。

这是我想要运行的脚本(

/home/update.sh
):

#!/bin/sh
netplan apply

在同一目录中,我通过运行使其可执行:

sudo chmod +x update.sh

我通过在

/etc/udev/rules.d/70-snap.snapd.rules
后面添加以下行来更新 udev 规则文件 (
LABEL="mm_mbm_check"
):

ACTION=="add", ATTRS{idVendor}=="`****`", ATTRS{idProduct}=="`****`", RUN+="/home/update.sh"

据我了解,我使用 * 来允许任何 USB 设备。

更新此 udev 规则文件后,我运行以下脚本来应用规则:

sudo service udev restart

这是运行重启 udev 命令后来自 syslog 的日志 (

tail -f /var/log/syslog
):

Nov  8 19:52:20 ubuntu systemd-udevd[23328]: /usr/lib/udev/rules.d/90-pi-bluetooth.rules:14 Invalid value "/bin/sh -c 'ALIASES=/proc/device-tree/aliases; if cmp -s $ALIASES/uart0 $ALIASES/serial0; then echo 0;elif cmp -s $ALIASES/uart0 $ALIASES/serial1; then echo 1; else exit 1; fi'" for PROGRAM (char 58: invalid substitution type), ignoring, but please fix it.
Nov  8 19:52:20 ubuntu systemd-udevd[23328]: /usr/lib/udev/rules.d/90-pi-bluetooth.rules:27 Invalid value "/bin/sh -c 'ALIASES=/proc/device-tree/aliases; if [ -e /dev/ttyAMA0 ]; then exit 1; elif cmp -s $ALIASES/uart0 $ALIASES/serial0; then echo 0;elif cmp -s $ALIASES/uart0 $ALIASES/serial1; then echo 1; else exit 1; fi'" for PROGRAM (char 97: invalid substitution type), ignoring, but please fix it.
Nov  8 19:52:20 ubuntu systemd-udevd[23328]: /usr/lib/udev/rules.d/90-pi-bluetooth.rules:38 Invalid value "/bin/sh -c 'ALIASES=/proc/device-tree/aliases; if cmp -s $ALIASES/uart1 $ALIASES/serial0; then echo 0; elif cmp -s $ALIASES/uart1 $ALIASES/serial1; then echo 1; else exit 1; fi '" for PROGRAM (char 58: invalid substitution type), ignoring, but please fix it.

最后,当我将 USB 设备插入 Raspberry 时,没有任何反应。

我的配置:

  • 树莓派3
  • Ubuntu 服务器 21.10

感谢您的帮助!

shell ubuntu usb udev
2个回答
1
投票

一切都正确,除了文件中添加的规则

/etc/udev/rules.d/70-snap.snapd.rules

为了使其正常工作,我在

90-usb.rules
目录中添加了一个新文件
/etc/udev/rules.d/
,其中包含以下规则:

ACTION=="add", ATTRS{idVendor}=="****", ATTRS{idProduct}=="****", RUN+="/home/update.sh"

而不是

ACTION=="add", ATTRS{idVendor}=="`****`", ATTRS{idProduct}=="`****`", RUN+="/home/update.sh"

我使用以下脚本重新启动了 udev:

sudo service udev restart

现在,当我插入任何 USB 设备时,我的脚本就会运行。


0
投票

我找到了使用已安装 USB 密钥内容的解决方案

  1. 添加插入 USB 闪存驱动器时启动的脚本:

nano /home/user/usbhook.sh

#!/usr/bin/env bash
# Debugging test script called by udev

# File paths, to be customized as needed
logFile="/tmp/logFileUsbHook.txt"
dateFile="/tmp/.logDateUsbHook.date"

# Ensure only one instance is running at a time
instances=$(lsof -t "$0" | wc -l)
if (( $instances > 1 )); then exit 1; fi

# Prevent multiple executions inside a 3-second time frame
if [ ! -f $dateFile ]; then
    echo "lastExecution=0" > $dateFile
fi
source $dateFile # reads the $lastExecution var from file
timeframe=$(( $(date +%s) - $lastExecution )) # seconds from last execution
if (( $timeframe <= 3 )); then exit 1; fi
echo "lastExecution=$(date +%s)" > $dateFile

# Debugging info
echo "USB Device '$1' plugged in..." >> $logFile

sleep 5

# Retrieve the mount point of the USB device based on the device name
device_name="$1"
usb_mount_point=$(grep "$device_name" /etc/mtab | awk '{print $2}')

# Check if the USB device is mounted
if [ -n "$usb_mount_point" ]; then
    # Access the directory of the USB device
    cd "$usb_mount_point"
    echo "USB device directory: $usb_mount_point" >> $logFile
     # Do stuff with the content of your usb key
else
    echo "No mount point found for the device $device_name." >> $logFile
fi

# Do other stuff...
exit 0

不要忘记添加执行:

chmod +x /home/user/usbhook.sh

  1. 添加 udev 规则以触发 USB Key 连接:

sudo nano /etc/udev/rules.d/99-usbhook.rules

ACTION=="add", KERNEL=="sd*", SUBSYSTEMS=="usb", TAG+="systemd", ENV{SYSTEMD_WANTS}="dummy@%k.service"

重新加载规则:

sudo udevadm control --reload-rules

  1. 添加服务:

sudo nano /etc/systemd/system/[email protected]

[Unit]
Description=Run script on USB plug

[Service]
Type=oneshot
ExecStart=/home/simon/usbhook.sh %i
RemainAfterExit=no

充值服务:

systemctl daemon-reload

© www.soinside.com 2019 - 2024. All rights reserved.