使用inotifywait在BASH中触发事件

时间:2015-10-05 00:07:40

标签: bash inotifywait

我正在监控一个日志文件(它是一个PBX队列文件,当电话进来时它被写入,它是调用发生的结果,无论是呼叫者挂断等等)

以下是我所拥有的:

while inotifywait -m -e close_write /var/log/asterisk/queue_log_test;
do
  if [ tail -n1 /var/log/asterisk/queue_log | grep EXITWITHTIMEOUT ];
    then
            php /usr/local/scripts/queue_monitor/pbx_queue_timeout.php
  elif [ tail -n1 /var/log/asterisk/queue_log | grep ABANDON ];
    then
            php /usr/local/scripts/queue_monitor/pbx_queue_abandon.php
  elif [ tail -n1 /var/log/asterisk/queue_log | grep COMPLETE ];
    then
            php /usr/local/scripts/queue_monitor/pbx_queue_complete.php
  else
    # Don't do anything unless we've found one of those
    :
  fi
done

现在,如果我运行脚本,它会成功设置手表并检测更改/关闭(我已经尝试了MODIFY和CLOSE_WRITE,两者都有效)

Setting up watches.
Watches established.
/var/log/asterisk/queue_log_test CLOSE_WRITE,CLOSE

但事件永远不会被触发(我已经在inotify脚本之外测试了PHP脚本,并且它们执行得非常出色)

如果我亲手操作正在观看的文件,它会成功并找到短语:

[root@pbx ...local/scripts/queue_monitor]: tail /var/log/asterisk/queue_log_test
ABANDON
[Load: 0.00] [Time: 19:04:43]
[root@pbx ...local/scripts/queue_monitor]:

我在这里失踪了什么?

1 个答案:

答案 0 :(得分:1)

您正在使用-m的{​​{1}}开关,这会让它无限期地运行:

inotifywait

while循环正在等待它完成,所以它可以评估是退出代码来决定循环是否应该继续。