Search code examples
linuxloggingtail

How do I exit tail --follow on ERROR or FATAL log level


I tail -f the log file of one of my services, but I would like to automatically stop my tail process once my logger has printed any content starting with OR containing ERROR or FATAL.

How can I achieve this?


Solution

  • To terminate tail -f immediately after the output has a line containing ERROR or FATAL, try:

    tail -f file.log | awk '{print} /ERROR|FATAL/{exit}'
    

    Example

    $ cat file.log 
    abc
    def
    ghi
    ERROR
    jkl
    mno
    pqr
    $ tail -f file.log | awk '{print} /ERROR|FATAL/{exit}'
    abc
    def
    ghi
    ERROR