服务器服务监测脚本

WBOY
풀어 주다: 2016-06-07 16:32:02
원래의
888명이 탐색했습니다.

由于最近一直在弄服务器,所以一直没有带来Arch Linux的东西,这篇文章给出的脚本依旧是为服务器准备的。写shell多了,C都快忘了怎么写了,这样不好

由于最近一直在弄服务器,所以一直没有带来Arch Linux的东西,这篇文章给出的脚本依旧是为服务器准备的。写shell多了,C都快忘了怎么写了,这样不好啊……

公司的服务器经常莫名其妙就挂,ssh上去又是好的,看看进程发现服务根本没打开,为什么呢?服务器被重启了!我的前任留下一个check.sh脚本,用来监测apache等进程,脚本在监测到进程过多时就重启服务器,结果有些服务器没有设置好开机启动那些apache之类的,就悲剧了。我就重新写了一个脚本,代替了crontab中原来的check.sh,在进程过多时不是重启服务器而是重启服务,这下服务器正常多了,网站那边负责的朝我喊“XXXX网站挂了,什么问题?”少了,哈哈。

废话多了点,脚本如下,使用前请一定看清代码并且修改其中一些路径之类的东西,以确保不会在你的服务器上造成什么不必要的伤害。

[bash]
#!/bin/bash
# service –> name to test
# apache –> apache|apache2|http|httpd
# nginx –> nginx
# mysql –> mysql|mysqld
# php-cgi –> php-cgi
# vsftpd –> vsftp|vsftpd
# pure-ftpd –> pure-ftp|pure-ftpd
# apache-tomcat –> java
# Set the service name you want to test and its max number of processes first.
# Don’t forget to check the bin path for each service.
# By Chen Zhidong
# http://sillydong.com

# This is a special one for the apache path is /usr/sbin

SERVICE=”apache2 mysqld vsftpd”
SERVICE_MAX=(35 5 5)

LOGPATH=”/root”
#APACHE_PATH=”/usr/sbin”
#MYSQL_PATH=”/usr/local/mysql”
#NGINX_PATH=”/usr/local/nginx”
#PHP_PATH=”/usr/local/php”
#TOMCAT_PATH=”/usr/local/apache-tomcat-6.0.26″
#PURE_PATH=”/usr/local/pureftpd”
i=0

if [ -e $LOGPATH/restart.log ]
then
tail -n 10 $LOGPATH/restart.log > $LOGPATH/restart.tmp
rm -rf $LOGPATH/restart.log
mv $LOGPATH/restat.tmp $LOGPATH/restart.log
fi

for serv in $SERVICE
do
#echo “$serv”
#echo “$i”
#echo “${SERVICE_MAX[i]}”

counter=0
counter=`ps -A | grep “$serv” | wc -l`
#echo “$counter”

if [ $counter -eq 0 ];then
echo “$serv is off at $(date), starting…” >> $LOGPATH/restart.log
case $serv in
apache*|http*)
#echo “apache start”
$APACHE_PATH/bin/apachectl restart >> $LOGPATH/restart.log
;;
nginx)
#echo “nginx start”
$NGINX_PATH/sbin/nginx -s reopen >> $LOGPATH/restart.log
;;
mysql*)
#echo “mysql start”
if [ -e /etc/init.d/mysqld ];
then
/etc/init.d/mysqld restart >> $LOGPATH/restart.log
else
/etc/init.d/mysql restart >> $LOGPATH/restart.log
fi
;;
php-cgi)
#echo “php-cgi start”
$PHP_PATH/sbin/php-fpm restart >> $LOGPATH/restart.log
;;
vsftp*)
#echo “vsftpd start”
/etc/init.d/vsftpd restart >> $LOGPATH/restart.log
;;
pure-ftp*)
#echo “pureftpd start”
$PURE_PATH/sbin/pure-ftpd -B >> $LOGPATH/restart.log
;;
java*) #apache-tomcat
#echo “tomcat start”
$TOMCAT_PATH/bin/startup.sh >> $LOGPATH/restart.log
;;
*)
echo “Wrong service name while starting…” >> $LOGPATH/restart.log
;;
esac
else
echo “$serv is on at $(date), next service…” >> $LOGPATH/restart.log
fi

if [ $counter -gt ${SERVICE_MAX[i]} ];then
echo “$(date), too many $serv, needs to restart…” >> $LOGPATH/restart.log
case $serv in
apache*|http*)
#echo “apache restart”
$APACHE_PATH/bin/apachectl restart >> $LOGPATH/restart.log
;;
nginx)
#echo “nginx restart”
$NGINX_PATH/sbin/nginx -s reload >> $LOGPATH/restart.log
;;
mysql*)
#echo “mysql restart”
if [ -e /etc/init.d/mysqld ];
then
/etc/init.d/mysqld restart >> $LOGPATH/restart.log
else
/etc/init.d/mysql restart >> $LOGPATH/restart.log
fi
;;
php-cgi)
#echo “php-cgi restart”
$PHP_PATH/sbin/php-fpm restart >> $LOGPATH/restart.log
;;
vsftp*)
#echo “vsftpd restart”
/etc/init.d/vsftp* restart >> $LOGPATH/restart.log
;;
pure-ftp*)
#echo “pureftpd restart”
$PURE_PATH/sbin/pure-ftpd -B >> $LOGPATH/restart.log
;;
java*) #apache-tomcat
#echo “tomcat restart”
$TOMCAT_PATH/bin/startup.sh >> $LOGPATH/restart.log
;;
*)
echo “Warning: Wrong service name while restartiing…” >> $LOGPATH/restart.log
;;
esac
fi

let “i++”
done

exit 0

#End
[/bash]

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿