使用 inotifywait 在 BASH 中触发事件

Triggering Event in BASH with inotifywait

我正在监控一个日志文件(它是一个 PBX queue 文件,它会在来电时被写入,它是呼叫发生的情况、呼叫者是否挂断等的结果)

这是我拥有的:

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

现在,如果我 运行 脚本,它会成功设置手表并检测到 change/close(我已经尝试了 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]:

我在这里缺少什么?

您正在使用 inotifywait-m 开关,这使得它 运行 无限期:

   -m, --monitor
          Instead of exiting  after  receiving  a  single  event,  execute
          indefinitely.   The default behaviour is to exit after the first
          event occurs.

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