1, monitor the shell script code of the HTTPD status code.
#!/bin/sh#site:www.jquerycn.cn#website[0]=www.jquerycn.cn/chuzu/'#网站1mobile[0]='13141200000'#对应网站1 Mobile phone number website[1]=www.jquerycn.cn/chushou/'#同上2mobile[1]='13141200000'#同上2 # When the site is large, consider storing it as a file or reading the length from the database=${#website [@]} #获取网站总数量 for((i=0; i< $length; i++)) #循环执行 DoStatus=$ (Curl-i-MTen-o/dev/NULL-s-w%{http_code} ${website[$i]}) #CURL get the HTTP status codeif["$status"X! =" $"X]; Then #检测是否为 $(normal) echo ${website[$i]}'='$status #php/htdoc/jk/shell_monitor.php ${mobile[$i]} ${website[$i]}'=>accesserror!'#执行PHP文件 (using a third-party SMS library, or sending alarm messages) fi #结束ifdone #结束 Do
Execution: CRONTAB-E
Write content: 5 * * * */shell path
(Executes once in 5 minutes)
2. Shell script to monitor website status
Monitor the status of the specified Web page, if return 200 means normal, otherwise error:
#!/bin/bash #edit: www.jquerycn.cn# website URL address #url=$1#获取http响应代码 Http_code= ' Curl-o/dev/NULL-s-w"%{http_code}" "${url}"' #服务器能正常响应, should return 200 of the codeif[$HTTP _code! = $];then Echo $HTTP _code ((a= $HTTP _code/Ten) Echo $a # The exit $a can be handled by the alarmElseExit0fi
Methods to invoke the script:
Bash./url_monitor.sh "http://www.jquerycn.cn/"
Set up crontab Scheduled tasks to be performed every five minutes to monitor the health of the site.
Example:
5 * * * *./url_monitor.sh
Shell script to monitor the state of the Linux server website