-1

I'm monitoring a log file (it's a PBX queue file, it's written to when a call comes in and it's the result of what happens to the call, whether the caller hangs up, etc)

Here's what I have:

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

Now, if I run the script, it successfully sets up the watches and detects the change/close (I've tried both MODIFY and CLOSE_WRITE, both work)

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

But the event is never triggered (I have tested the PHP scripts outside of the inotify script and they execute splendidly)

If I run a tail by hand of the file that's being watched, it's successful and finds the phrase:

[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]:

What is it I'm missing here?

TD46
  • 13
  • 2
  • You're running inotifywait and watching `queue_log_test` but testing for changes in `queue_log`. Is there some reason these 2 files are different? – Joe Young Oct 05 '15 at 00:18
  • Hi Joe: Sorry, that's a typo, both files existed. I created the test file because the actual queue_log file is written to by the PBX software, I didn't want to interfere with it. Either will work – TD46 Oct 05 '15 at 19:54

1 Answers1

1

You are using the -m switch of inotifywait, which makes it run indefinitely:

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

And the while loop is waiting for it to finish, so it can evaluate is exit code to decide if the loop should continue or not.

Alvaro Gutierrez Perez
  • 3,669
  • 1
  • 16
  • 24
  • Ok, the reason I was using monitor was because I want it to run indefinitely. IE: If it finds the event, trigger and then continue monitoring the file for future events. Is there a better suited way to do that? Now that you say it it makes sense I didn't think of it that way – TD46 Oct 05 '15 at 19:56
  • @TD46 simply remove the `-m` and it will run indefinitely, as you are inside a while loop and `inotifywait` returns true if it triggers an event – Alvaro Gutierrez Perez Oct 05 '15 at 20:37
  • Alright let me pop onto that server and give it a whirl – TD46 Oct 05 '15 at 20:54
  • So that seems to have worked. Problem being now it appears I have a syntax error in the elif statements. `/var/log/asterisk/queue_log_test MODIFY ./freepbx_queue_monitor.sh: line 13: [: missing `]' grep: ]: No such file or directory ./freepbx_queue_monitor.sh: line 16: [: missing `]' grep: ]: No such file or directory ./freepbx_queue_monitor.sh: line 19: [: missing `]' grep: ]: No such file or directory Setting up watches. Watches established.` I placed the GREP queries in single quotes and ensured I had spaces before the brackets on both sides of the if/elif expression – TD46 Oct 05 '15 at 21:40
  • You seem to be missing some closing brackets: `]`. Also `grep` is not finding requested files. – Alvaro Gutierrez Perez Oct 05 '15 at 21:53
  • Ended up removing the brackets and resolving issue. Thanks for the help – TD46 Oct 06 '15 at 01:40