CentOS下使用shell脚本监控网站是否正常

一个监控脚本,每分钟检测一次,如果无法访问就重启服务。
1. 编写Shell脚本,保存到/opt/http_monitor
#!/bin/bash
#网站url地址
URL=”http://www.xxx.com/”
#获取http响应代码
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
#echo $HTTP_CODE
#服务器能正常响应,应该返回200的代码
if [ $HTTP_CODE != '200' ];then
#重启服务
service httpd restart
fi
2. 增加到crontab,使用crontab -e命令添加新的任务:
Shell代码
#每分钟运行一次
*/1 * * * * /opt/http_monitor
3. 停掉服务,测试,cool!一分钟后自动启动了!可是中文乱码了,查了一下原来是没有加载系统变量,好办,在http_monitor中加入locale环境变量就可以了:
Shell代码
export LC_ALL=zh_CN.UTF-8

猜你喜欢

转载自whxhz.iteye.com/blog/1084487