Today, due to business needs, to check whether the company's website can be accessed by the external network, under the guidance of colleagues to do a planning task, first of all need to understand the needs:
Visit the company website every five minutes once, if the access is successful, save the access record to the log, and if the access fails, send the message to the specified mailbox.
Need to get, how to achieve it?
How to achieve this function, in the next dull, do not think, certainly is crontab this function, so directly write a script (level limited to this morning), the script is as follows:
[Email protected]
*/5 * * * * Curl http://www.baidu.com
This certainly not, and finally I asked some active in the group of Daniel, they gave guidance, and finally combined with their comments and their own search data finally resolved, the script content is as follows:
#! /bin/bash
#定义变量
L= "web is good!"
#获取网站状态特征码 @1
Ll= ' curl-i-S http://www.baidu.com | head-1 | Cut-d ""-f2 "
#做判断
if [$LL = "200"]
Then
echo "$L" >>/tmp/acc ' date +%f/%h/%m '. Log
elif [$LL = "301"]
Then
echo "$L" >>/tmp/acc ' date +%f/%h/%m '. Log
elif [$LL = "302"]
Then
echo "$L" >>/tmp/acc ' date +%f/%h/%m '. Log
Else
echo "not find the page." | Mail-s "not find the page" "[Email protected]"
Fi
@1: Web Site Status Signature: If the returned value is 200, 301, 302 represents a successful visit to the site
The script is finished, and the following is a much simpler task to write.
Crontab-e
*/5 * * * */usr/jihuarenwu.sh
Note that the jihuarenwu.sh to have execute permissions.
Because the level is low now, have found new method will revise immediately, if Netizen has good method please inform.
In this thanks: Guangzhou-greedy, Shenzhen-sam, Nanjing-Yun, Amin, Jiangsu-Kite, Royal Park + bean dish, wait, Budweiser little Prince and other friends of the guidance.
Scripting: Access a website every 5 minutes, if the access is successful, save the access record to the log, if the access fails, send mail to the specified mailbox