I want to monitor the logs continously and echo the output if keyword appears then continues the monitoring!!

My question is how to perform the below operation

Tail the logs. If keyword[down] matches then echo, else continue monitoring.

I tried this:

tail -F  /data/log/test.log |
egrep -io 'got signal 15 | now exiting' | 
while read -r line ;
do
    case "$line" in
       "got signal 15")
        echo "hi"
            ;;
        "now exiting")
        echo "hi2"
             ;;
        *)
    esac
done

but its not working properly with tail statement as the same script works properly if i use cat or less

Please suggest what i did wrong here.

closed as off-topic by Jeff Schaller, Tomasz, mosvy, Christopher, Anthony Geoghegan Nov 8 at 23:25

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What do you mean by "not working properly"? – thriqon Nov 8 at 20:00
  • 2
    The pipe line | is buffering for efficiency. You either need to use unbuffer egrep ... or stdbuf -oL egrep '...' | while... This is what I'm observing at the first glance. – Valentin Bajrami Nov 8 at 21:08
  • seems this one works as a charm egrep --line-buffered -io 'got signal 15|now exiting' – vignesh Nov 8 at 21:15
  • 1
    @ValentinBajrami Thank you for the suggestion, seems buffering was the problem – vignesh Nov 8 at 21:35