udev规则多次运行bash脚本

时间:2013-11-12 18:55:40

标签: linux bash udev

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

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

然而,脚本运行了几次而不是一次,我认为它已经检测到了硬件?我尝试将睡眠10放入脚本和fi中,但它没有任何区别。

1 个答案:

答案 0 :(得分:0)

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