【问题标题】:udev rule runs bash script multiple timesudev 规则多次运行 bash 脚本
【发布时间】:2013-11-25 02:21:26
【问题描述】:

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

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

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

【问题讨论】:

  • 我看到您正在追加到 RUN 列表 - 您是否有可能多次这样做?
  • 它是否为 USB 链中的每个设备运行一次?即如果有四个父设备/集线器,它会运行四次吗?
  • 对这两个问题都没有,我发现这篇文章 ubuntuforums.org/showthread.php?t=1747496 但不确定它与我的 udev 规则有什么关系

标签: linux bash udev


【解决方案1】:

这不是解决方案,而是解决方法:
一种方法(简单)是像这样开始你的脚本“/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"

【讨论】:

    猜你喜欢
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    相关资源
    最近更新 更多