tail -f to several files piped to "while read" gives unexpected results

Viewed 73

I wrote a quick bash script to monitor several log files and send their contents to a Mattermost chat. This is the script:

#!/bin/bash 

# HOOKS
hook1=...#Mattermost hook
hook2=...#Mattermost hook
hook3=...#Mattermost hook

date=$1
year=$2

unbuffer tail -f -n0 /var/log/$year/hook1_*$date.log \
  /var/log/$year/hook2_*$date.log \
  /var/log/$year/hook3_*$date.log | grep "ERROR" | while read -r msg
  do
    if [[ $msg = *"hook1"* ]]
    then
      hook=$hook1
    elif [[ $msg = *"hook2"* ]]
    then
      hook=$hook2
    else
      hook=$hook3
    fi
    msg=$(echo "$msg" | sed -r "s/\"/\\\\\"/g")
    curl -i -X POST --data-urlencode "payload={\"text\": \"$msg\"}" $hook

  done

The sed part escapes quotes so I can replace the message into curl.

However, several problems arise during execution:

  • There seems to be 2 processes in htop after I run the script once.
  • Sometimes messages are not sent to Mattermost as they appear, but instead they come out in batches.
  • Messages sometimes appear cut and some content is lost.
0 Answers
Related