linux - udev 规则多次运行 bash 脚本

标签 linux bash udev

我创建了一个 udev 规则来在插入一个 usb 设备后执行一个 bash 脚本

SUBSYSTEMS=="usb", ATTRS{serial}=="00000000", SYMLINK+="Kingston", RUN+="/bin/flashled.sh"

但是脚本运行了多次而不是一次,我认为这是检测到硬件的方式?我尝试将 sleep 10 放入脚本和 fi 中,但没有任何区别。

最佳答案

这不是解决方案,而是解决方法:
一种方法(简单)是像这样开始你的脚本“/bin/flashled.sh”

#!/bin/bash
#this file is /bin/flashled.sh

#let's exit if another instance is already running
if [[ $(pgrep -c "$0" ) -gt 1 ]]; then exit ;fi

... ... ...

但是,在某些边界情况下,这可能有点容易出现竞争条件(bash 有点慢,因此无法确保它始终有效)但它可能在您的情况下完美运行。

另一个(更可靠但代码更多)是这样开始“/bin/flashled.sh”:

#!/bin/bash
#this file is /bin/flashled.sh
#write in your /etc/rc.local: /bin/flashled.sh & ; disown
#or let it start by init.    

while :
do
    kill -SIGSTOP $$ # halt and wait
    ... ...          # your commands here
    sleep $TIME      # choose your own value here instead of $TIME
done

在引导期间启动它(例如,通过/etc/rc.local),因此它将等待信号继续......它得到多少“继续”信号并不重要(它们不是排队),只要它们在 $TIME 内

相应地更改您的 udev 规则:

SUBSYSTEMS=="usb", ATTRS{serial}=="00000000", SYMLINK+="Kingston", RUN+="/usr/bin/pkill -SIGCONT flashled.sh"

关于linux - udev 规则多次运行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19937584/

相关文章:

bash - 在基于 cygwin 的 shell 脚本中使用 ffmpeg 连接 mkv 文件时出错

linux - udev中键盘的 `KEY`属性解读

c++ - 如何从终端标准输入重定向到进程标准输入

regex - 用于匹配文件中的 ip 地址的 Grep 正则表达式

linux - 在一个域下的不同机器上托管邮件服务器和网站

linux - 在 Linux 中比较值

linux - uWSGI 配置使用 HTTPS

linux - Sed 无法将临时文件复制到原始文件

linux - udev规则匹配雷凌wifi卡加载rt2800usb

linux - 设置 udev 时 apt-get 升级卡住