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

前端 未结 5 680
梦毁少年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:14

    I also found that you can use awk to monitor for pattern and perform some action when pattern is found:

    tail -fn0 logfile | awk '/pattern/ { print | "command" }'
    

    This will execute command when pattern is found in the log. Command can be any unix command including shell scripts or anything else.

提交回复
热议问题