这个是我2022年年底回家过年,而那段时间服务器偶尔会被攻击,偶尔会宕机,家里又处于偏远地区,没有网络,也重启不了环境,如果期间宕机,那么可能是10多天的宕机,虽然没啥损失,但是一会影响SEO,二会影响需要到我网站查阅代码的用户,所以就想着让服务器能有自己重启环境的能力。
所以,我添加了几个进程守护脚本,放到宝塔计划任务,保证环境不死,不是宝塔的也可以进行参照修改,代码如下,执行频率为一分钟:
1、数据库进程守护
#!/bin/bashstatus=`service mysqld status | grep running | wc -l`port=`netstat -lnt | grep 3306 | wc -l`pid=`ps -ef | grep mysql | grep -v grep | wc -l`if [ $status -ne 1 ] && $port -ne 1 ] && [ $pid -ne 2 ];then/etc/init.d/mysqld restartelseecho "mysql running!!!"fi#!/bin/bash status=`service mysqld status | grep running | wc -l` port=`netstat -lnt | grep 3306 | wc -l` pid=`ps -ef | grep mysql | grep -v grep | wc -l` if [ $status -ne 1 ] && $port -ne 1 ] && [ $pid -ne 2 ];then /etc/init.d/mysqld restart else echo "mysql running!!!" fi#!/bin/bash status=`service mysqld status | grep running | wc -l` port=`netstat -lnt | grep 3306 | wc -l` pid=`ps -ef | grep mysql | grep -v grep | wc -l` if [ $status -ne 1 ] && $port -ne 1 ] && [ $pid -ne 2 ];then /etc/init.d/mysqld restart else echo "mysql running!!!" fi
2、nginx进程守护
#!/bin/bashpid=`ps -ef | grep nginx | grep root | grep /www/server/nginx/sbin/nginx | grep /www/server/nginx/conf/nginx.conf | wc -l`status=`service nginx status | grep running | wc -l`if [ $pid -ne 1 ] && [ $status -ne 1 ];then/etc/init.d/nginx restartelseecho "nginx running!!!"fi#!/bin/bash pid=`ps -ef | grep nginx | grep root | grep /www/server/nginx/sbin/nginx | grep /www/server/nginx/conf/nginx.conf | wc -l` status=`service nginx status | grep running | wc -l` if [ $pid -ne 1 ] && [ $status -ne 1 ];then /etc/init.d/nginx restart else echo "nginx running!!!" fi#!/bin/bash pid=`ps -ef | grep nginx | grep root | grep /www/server/nginx/sbin/nginx | grep /www/server/nginx/conf/nginx.conf | wc -l` status=`service nginx status | grep running | wc -l` if [ $pid -ne 1 ] && [ $status -ne 1 ];then /etc/init.d/nginx restart else echo "nginx running!!!" fi
3、PHP进程守护
以下为PHP5.6,如果添加其他的,请修改56,比如8.0将56修改为80。
#!/bin/bashpid=`ps -ef | grep php-fpm | grep root | grep process | grep master | grep 56 | wc -l`status=`service php-fpm-56 status | grep running | wc -l`if [ $pid -ne 1 ] && [ $status -ne 1 ];then/etc/init.d/php-fpm-56 restartelseecho "php56 running!!!"fi#!/bin/bash pid=`ps -ef | grep php-fpm | grep root | grep process | grep master | grep 56 | wc -l` status=`service php-fpm-56 status | grep running | wc -l` if [ $pid -ne 1 ] && [ $status -ne 1 ];then /etc/init.d/php-fpm-56 restart else echo "php56 running!!!" fi#!/bin/bash pid=`ps -ef | grep php-fpm | grep root | grep process | grep master | grep 56 | wc -l` status=`service php-fpm-56 status | grep running | wc -l` if [ $pid -ne 1 ] && [ $status -ne 1 ];then /etc/init.d/php-fpm-56 restart else echo "php56 running!!!" fi
4、memcached进程守护
#!/bin/bashpid=`ps -ef | grep memcached | wc -l`if [ $pid -ne 2 ];then/etc/init.d/memcached restartelseecho "memcached running!!!"fi#!/bin/bash pid=`ps -ef | grep memcached | wc -l` if [ $pid -ne 2 ];then /etc/init.d/memcached restart else echo "memcached running!!!" fi#!/bin/bash pid=`ps -ef | grep memcached | wc -l` if [ $pid -ne 2 ];then /etc/init.d/memcached restart else echo "memcached running!!!" fi
5、凌晨环境重启
根据自己需要添加。
#!/bin/bash/etc/init.d/memcached restart >> /dev/null/etc/init.d/php-fpm-81 restart >> /dev/null/etc/init.d/php-fpm-80 restart >> /dev/null/etc/init.d/php-fpm-74 restart >> /dev/null/etc/init.d/php-fpm-73 restart >> /dev/null/etc/init.d/php-fpm-72 restart >> /dev/null/etc/init.d/php-fpm-70 restart >> /dev/null/etc/init.d/php-fpm-56 restart >> /dev/null/etc/init.d/nginx restart >> /dev/null/etc/init.d/mysqld restart >> /dev/nullecho 已执行环境重启!#!/bin/bash /etc/init.d/memcached restart >> /dev/null /etc/init.d/php-fpm-81 restart >> /dev/null /etc/init.d/php-fpm-80 restart >> /dev/null /etc/init.d/php-fpm-74 restart >> /dev/null /etc/init.d/php-fpm-73 restart >> /dev/null /etc/init.d/php-fpm-72 restart >> /dev/null /etc/init.d/php-fpm-70 restart >> /dev/null /etc/init.d/php-fpm-56 restart >> /dev/null /etc/init.d/nginx restart >> /dev/null /etc/init.d/mysqld restart >> /dev/null echo 已执行环境重启!#!/bin/bash /etc/init.d/memcached restart >> /dev/null /etc/init.d/php-fpm-81 restart >> /dev/null /etc/init.d/php-fpm-80 restart >> /dev/null /etc/init.d/php-fpm-74 restart >> /dev/null /etc/init.d/php-fpm-73 restart >> /dev/null /etc/init.d/php-fpm-72 restart >> /dev/null /etc/init.d/php-fpm-70 restart >> /dev/null /etc/init.d/php-fpm-56 restart >> /dev/null /etc/init.d/nginx restart >> /dev/null /etc/init.d/mysqld restart >> /dev/null echo 已执行环境重启!
其实还有环境不全,因为我没用,所以就没写,你也可以按照我的脚本写一个,比如Redis。
© 版权声明
THE END