I have searched in all threads but I don't know how to solve my very simple problem..
I think is very simple but I'm not a bash expert..
I need a bash script that executes a continuous tail in a logfile and when grep the word "fail", execute another command and send me a mail (but the tail command must not stop)
I think I need for a "for loop" or "while read" or "if then else" commands. I tried different ways but couldn't find a working script..
Many thanks in advance for your help!
Most likely, it is simpler and better to use standard Linux utilities, instead of implementing this functionality with bash:
tail -f log-file | stdbuf -oL grep fail | xargs -L1 send-mail-alert
You have already figured out that you need tail/grep. You need two additional ingredients: stdbuf and xargs
The stdbuf will force 'line buffering', resulting in the 'fail' messages sent to the 'xargs' program without (buffering) delay.
The 'xargs' command will activate the script 'send-mail-alert' on every occurrence of 'fail'. You can inline the mail delivery, and use a script