例8
需求:lnmp服务器,访问状态码出现502,重启php-fpm消失,脚本监控出现502重启php-fpm
使用的命令:tail;while;grep -c;pgrep -l
#!/bin/bash
log=/data/log/access.log
while :
do
n=`tail -n100 $log | grep -c '502'`
if [ -z $n ]
then
exit
fi
if [ $n -gt 100 ]
then
/etc/init.d/php-fpm restart >/dev/null 2>/tmp/php-fpm.err
fpm=`pgrep -l php-fpm | wc -l`
if [ $fpm -eq 0 ]
then
echo "重启不成功"
exit
fi
fi
sleep 10
done
知识点:
grep -c 表示统计关键字出现的次数
pgrep -l 表示过滤进程和PID
pgrep -n 表示开始的PID
pgrep -o 表示结束的PID