有時候php業務的網站突然無法訪問,我們會去查找原因,首先會判斷是不是web服務器掛了,然後再去查詢是不是數據庫掛了,這裏提供一個腳本自動去檢測是web服務器和數據庫服務器,去確定到底是哪個服務器掛掉,並自動去啓動相對應的服務!
# vi check.sh
#!/bin/sh
cd /usr/local/sbin
test -e "checkweb.php" && rm -rf checkweb.php
test -e "checkdb.php" && rm -f checkdb.php
test -e "wget-log" && rm -f wget-log
test -e "wget-log.1" && rm -f wget-log.1
test -e "checkweb.php" && rm -rf checkweb.php
test -e "checkdb.php" && rm -f checkdb.php
test -e "wget-log" && rm -f wget-log
test -e "wget-log.1" && rm -f wget-log.1
wget -b "http://127.0.0.1/checkweb.php" > /dev/null 2>&1
wget -b "http://127.0.0.1/checkdb.php" > /dev/null 2>&1
sleep 2
wget -b "http://127.0.0.1/checkdb.php" > /dev/null 2>&1
sleep 2
checkweb=`cat checkweb.php`
checkdb=`cat checkdb.php`
checkdb=`cat checkdb.php`
if
[ "$checkweb" == "ok" ] ;then
echo "`date +%d/%m/%Y:%H:%M:%S` - - Webserver successfully!"
if
[ "$checkdb" == "ok" ] ;then
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected successfully!"
echo " "
echo "-------------------------------------------------------------"
elif
[ "$checkdb" != "ok" ] ;then
/etc/init.d/mysqld restart
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected bad!restart successful!"
curl "http://sms.api.bz/fetion.php?username=150********&password=******&sendto=150********&message=php連接數據庫失敗,數據庫重啓成功!"
echo " "
echo "-------------------------------------------------------------"
fi
exit 0
elif
[ "$checkweb" != "ok" ] ;then
killall -9 nginx
/usr/local/nginx/sbin/nginx
/usr/local/php-fcgi/sbin/php-fpm restart
echo "`date +%d/%m/%Y:%H:%M:%S` - - Webserver is down!restart successfully!"
wget -b "http://127.0.0.1/checkdb.php" > /dev/null 2>&1
sleep 2
checkdb2=`cat checkdb.php`
if
[ "$checkdb2" == "ok" ] ;then
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected successfully!"
curl "http://sms.api.bz/fetion.php?username=150********&password=******&sendto=150********&message=php服務器down機,重啓成功!"
echo " "
echo "-------------------------------------------------------------"
elif
[ "$checkdb2" != "ok" ] ;then
/etc/init.d/mysqld restart
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected bad!restart successful!"
curl "http://sms.api.bz/fetion.php?username=150********&password=******&sendto=150********&message=php服務器,mysql服務器down機,重啓成功!"
echo " "
echo "-------------------------------------------------------------"
fi
exit 0
[ "$checkweb" == "ok" ] ;then
echo "`date +%d/%m/%Y:%H:%M:%S` - - Webserver successfully!"
if
[ "$checkdb" == "ok" ] ;then
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected successfully!"
echo " "
echo "-------------------------------------------------------------"
elif
[ "$checkdb" != "ok" ] ;then
/etc/init.d/mysqld restart
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected bad!restart successful!"
curl "http://sms.api.bz/fetion.php?username=150********&password=******&sendto=150********&message=php連接數據庫失敗,數據庫重啓成功!"
echo " "
echo "-------------------------------------------------------------"
fi
exit 0
elif
[ "$checkweb" != "ok" ] ;then
killall -9 nginx
/usr/local/nginx/sbin/nginx
/usr/local/php-fcgi/sbin/php-fpm restart
echo "`date +%d/%m/%Y:%H:%M:%S` - - Webserver is down!restart successfully!"
wget -b "http://127.0.0.1/checkdb.php" > /dev/null 2>&1
sleep 2
checkdb2=`cat checkdb.php`
if
[ "$checkdb2" == "ok" ] ;then
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected successfully!"
curl "http://sms.api.bz/fetion.php?username=150********&password=******&sendto=150********&message=php服務器down機,重啓成功!"
echo " "
echo "-------------------------------------------------------------"
elif
[ "$checkdb2" != "ok" ] ;then
/etc/init.d/mysqld restart
echo "`date +%d/%m/%Y:%H:%M:%S` - - Mysql connected bad!restart successful!"
curl "http://sms.api.bz/fetion.php?username=150********&password=******&sendto=150********&message=php服務器,mysql服務器down機,重啓成功!"
echo " "
echo "-------------------------------------------------------------"
fi
exit 0
fi
checkweb.php內容:
<?php
echo "ok";
?>;
echo "ok";
?>;
checkdb.php內容:
<?php
//測試php數據庫鏈接狀態的腳本
$mysql_user = "dbuser";
$mysql_password = "dbpwd";
//測試php數據庫鏈接狀態的腳本
$mysql_user = "dbuser";
$mysql_password = "dbpwd";
$link = mysql_connect("localhost",$mysql_user,$mysql_password) or die("bad");
print ("ok");
mysql_close($link);
?>
print ("ok");
mysql_close($link);
?>
#crontab -e
*/2 * * * * /usr/local/sbin/check.sh >> /var/log/check.log