Сканирование файла в режиме реального времени и выполнение определенных команд при обнаружении 5 последовательных идентичных строк - PullRequest
0 голосов
/ 09 февраля 2012

В журнале Mysql есть что-то вроде:

1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1

Мне нужен скрипт оболочки bash, который будет выполнять следующие действия:

  1. Скрипт будет сканировать файл mysql.log в режиме реального времени (что-то вроде tail -F mysql.log)
  2. Если он встречает 5 последовательных идентичных строк, он вызовет некоторые команды (скажем, echo "yes")

1 Ответ

2 голосов
/ 09 февраля 2012
 awk '$0 != l { l=$0; c=0 }
      l=$0 {c++; if (c>=5) {system("YOURCOMMAND HERE")}}' LOGFILE

может сделать это для вас ... но что вы имеете в виду в реальном времени? Если он должен выполняться постоянно, настройте для него какой-нибудь механизм fifo или что-то в этом роде. Или просто сделайте:

 tail -F LOGFILE | <THEABOVE_AWK_SCRIPT>
...