Shell Tips: tail 监视日志输出

by Web全栈工程师 on 2011 年 12 月 01 日

使用tail -f 命令监视日志输出,如果有异常,执行命令:

方法一:

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

方法二:

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

http://stackoverflow.com/questions/4331309/shellscript-to-monitor-a-log-file-if-keyword-triggers-then-execute-a-command

Comments on this entry are closed.

Previous post:

Next post: