linux - 服务检测及实时重启

127 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

在实际生产中,我们经常遇到服务器宕机但是短时间内找不到原因的情况,这时可以设置一个实时检测并重启的脚本,以保证服务能够正常运行。

restart.sh脚本:

#!/bin/bash


m=$(netstat -ntlp|grep 80)


if [ ! "$m" ];then
        sh /..../startup.sh
        tm=$(date +'%Y/%m/%d %H:%M:%S')
        echo 'restart : '$tm >> /../start.log
fi

加入定时任务:

// 授权
chmod 777 restart.sh

// 加入定时任务,每分钟检测一次
crontab -e
*/1 * * * * /.../restart.sh

// 重新载入crontab
/sbin/service crond reload

// 查看定时任务
crontab -l

在这里插入图片描述