0

我正在寻找一种更有效的方法来监视我的守护进程中的文件。我写了一个脚本来循环观看几个文件(/proc/btn_sw1/proc/btn_sw2)......我知道循环是一个坏主意,我没有意识到它会有多糟糕。

我的守护进程是通过 init 进程自动启动的,在它启动后我检查了top输出,我的进程是 #1,持续运行 ~17% CPU:

PID  PPID USER     STAT   VSZ %VSZ  %CPU COMMAND
1698     1 root     S     2196   0%  17% {resetd.sh} /bin/sh /etc/init.d/resetd

当按下/释放硬件按钮时,我的守护程序监视由键盘驱动程序设置的 /proc 条目(它们的值只是 1 或 0)。所以我需要知道这些文件的价值何时发生变化。

当文件的值改变时,有没有办法让我的守护进程被唤醒?注意:我不想在每次读取之间只睡 X 秒,因为我需要超时按下按钮的时间,而且我不想错过开始。

我当前的守护进程代码:

#!/bin/sh

proc1file=/proc/btn_sw1
proc2file=/proc/btn_sw2
BTN1VAL=$(cat $proc1file)
BTN2VAL=$(cat $proc2file)

tic=0
elap_time=0
reset_met=0

until [ $reset_met -gt 0 ]
do
  BTN1VAL=$(cat $proc1file)
  BTN2VAL=$(cat $proc2file)
  if [ $BTN1VAL -gt 0 ] && [ $BTN2VAL -gt 0 ]
    then
    tic=`date +%S`

    # Start the 10second loop, I'm ok with reading in here, but before this I'd like
    # to be sleeping or idle instead of constantly polling
    until [ $elap_time -ge 5 ] || [ $BTN1VAL -lt 1 ] || [ $BTN2VAL -lt 1 ]
    do
      BTN1VAL=$(cat $proc1file)
      BTN2VAL=$(cat $proc2file)
      toc=`date +%S`
      elap_time=`expr $toc - $tic`
    done
    if [ $elap_time -ge 5 ]
    then
      reset_met=1
    else
      elap_time=0
    fi
  fi

done
echo "Rebooting!"
reboot -f
4

1 回答 1

0

你需要这个inotify-tools包。(不确定它是否适用于 /proc 文件,但值得一试)。

像这样的东西:

until [ $reset_met -gt 0 ]
do
    inotifywait "$proc1file" "$proc2file"
    tic=$(date +%S)
    ...
done

编辑反引号语法已过时,推荐的语法是$(command).

于 2014-02-20T19:21:53.087 回答