search
HomeDatabaseMysql Tutorial服务器服务监测脚本

由于最近一直在弄服务器,所以一直没有带来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]

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

MySQL String Data Types: A Comprehensive GuideMySQL String Data Types: A Comprehensive GuideMay 08, 2025 am 12:14 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,idealforconsistentlengthdatalikecountrycodes;2)VARCHARforvariable-lengthstrings,suitableforfieldslikenames;3)TEXTtypesforlargertext,goodforblogpostsbutcanimpactperformance;4)BINARYandVARB

Mastering MySQL BLOBs: A Step-by-Step TutorialMastering MySQL BLOBs: A Step-by-Step TutorialMay 08, 2025 am 12:01 AM

TomasterMySQLBLOBs,followthesesteps:1)ChoosetheappropriateBLOBtype(TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOB)basedondatasize.2)InsertdatausingLOAD_FILEforefficiency.3)Storefilereferencesinsteadoffilestoimproveperformance.4)UseDUMPFILEtoretrieveandsaveBLOBsco

BLOB Data Type in MySQL: A Detailed Overview for DevelopersBLOB Data Type in MySQL: A Detailed Overview for DevelopersMay 07, 2025 pm 05:41 PM

BlobdatatypesinmysqlareusedforvoringLargebinarydatalikeImagesoraudio.1) Useblobtypes (tinyblobtolongblob) Basedondatasizeneeds. 2) Storeblobsin Perplate Petooptimize Performance.3) ConsidersxterNal Storage Forel Blob Romana DatabasesizerIndimprovebackupupe

How to Add Users to MySQL from the Command LineHow to Add Users to MySQL from the Command LineMay 07, 2025 pm 05:01 PM

ToadduserstoMySQLfromthecommandline,loginasroot,thenuseCREATEUSER'username'@'host'IDENTIFIEDBY'password';tocreateanewuser.GrantpermissionswithGRANTALLPRIVILEGESONdatabase.*TO'username'@'host';anduseFLUSHPRIVILEGES;toapplychanges.Alwaysusestrongpasswo

What Are the Different String Data Types in MySQL? A Detailed OverviewWhat Are the Different String Data Types in MySQL? A Detailed OverviewMay 07, 2025 pm 03:33 PM

MySQLofferseightstringdatatypes:CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM,andSET.1)CHARisfixed-length,idealforconsistentdatalikecountrycodes.2)VARCHARisvariable-length,efficientforvaryingdatalikenames.3)BINARYandVARBINARYstorebinarydata,similartoC

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.