Shellscript to monitor a log file if keyword triggers then execute a command?

前端 未结 5 669
梦毁少年i
梦毁少年i 2020-12-07 13:57

Is there a cheap way to monitor a log file like tail -f log.txt, then if something like [error] appears, execute a command?

Thank you.

5条回答
  •  隐瞒了意图╮
    2020-12-07 14:21

    tail -fn0 logfile | \
    while read line ; do
            echo "$line" | grep "pattern"
            if [ $? = 0 ]
            then
                    ... do something ...
            fi
    done
    

提交回复
热议问题