服务器端查看log的shell脚本

时间:2021-08-02 09:40:38

持续过滤log脚本

服务器端持续查看log的shell脚本(其中path1和path2替换为路径特征名,“tail -f”后面接的路径替换为路径特征名所对应的log文件路径):

#! /bin/sh
#Check log.
echo "Which path(1/2):"
read path
echo "Keyword:"
read keyword
if [ "$path" == '' ]; then
tail -f ../project/path1/log/xxx.log | grep -i "$keyword"
elif [ "$path" == '' ]; then
tail -f ../project/path2/log/xxx.log | grep -i "$keyword"
fi

整体过滤log脚本

#! /bin/sh
#Check log.
if [ "$1" == 'path1' ]; then
grep -i "$2" "../project/path1/log/xxx.log"
elif [ "$1" == 'path2' ]; then
grep -i "$2" "../project/path2/log/xxx.log"
fi

如何使用脚本

1. 进入服务器后在当前目录下touch checklog.sh文件;

2. 输入“i”,复制以上内容点击右键粘贴;

3. 按esc退出编辑模式,输入“:wq”保存修改;

4. 输入“sh checklog.sh”执行脚本;

5. 输入路径特征名和关键字对log进行持续过滤。