zoukankan      html  css  js  c++  java
  • 利用wget 和 curl 监控网站是否正常

    监控网站URL是否正常最常见的方法莫过于wget和curl命令了,这两个命令都是非常强大,参数也非常多,下面列举几个常用的参数。

    wget  常用命令参数:
    --spider                                        模拟爬虫的行为去访问网站,但不会下载网页
    -q --quite                                      安静的访问,禁止输出,类似-o /dev/null
    -o --output-file=FILE                    记录到输出文件
    -T --timeout=SECONDS             访问网站的超时时间
    -t -tries=NUMBER                        重试次数

    curl  常用命令参数:

    -I/--header                       显示响应头信息
    -m/--max-time<seconds>               访问超时时间
    -o/--output<file>                             记录访问信息到文件
    -s/--silent                                        静默模式访问,不输出信息
    -w/--write-output<format>              以固定格式输出,例如%{http_code},输出状态码

    实际监控网站我们可以利用curl 或者wget 的返回值判断网站是否正常:

    [root@localhost ~]# wget --spider -T 5 -q -t 2 www.baidu.com
    [root@localhost ~]# echo $?   利用返回值确定网站是否正常
    0  
    [root@localhost ~]# curl -s -o /dev/null www.baidu.com
    [root@localhost ~]# echo $?
    0
    获取命令执行后的状态码:
    [root@localhost ~]# curl -I -m 5 -s -w "%{http_code}
    " -o /dev/null  www.baidu.com
    200

    监控url shell脚本:

    #!/bin/bash
    
    usage() {
        echo $"useage:$0 url"
        exit 1
    }
    
    
    check_url() {
    
        curl -s -o /dev/null $1                   ## wget --spider -q -o /dev/null --tries=1 -T 5 $1 也可以使用wget获取返回值
        if [ $? -eq 0 ];then
            echo "$1 is ok"
            exit 0
        else
            echo "$1 is fail"
            exit 1
        fi
    }
    
    
    main() {
        if [ $# -ne 1 ];then
            usage
        fi
    
        check_url $1
    }
    
    main $*        #把所有命令行接收到的参数作为函数参数传给函数内部
  • 相关阅读:
    学习linux之用户-文件-权限操作
    Hadoop--Hadoop的机架感知
    redhat 6.3 64位安装中文输入法全过程记录
    hdu 4619 Warm up 2(并查集)
    openGL 初试 绘制三角形 和添加鼠标键盘事件
    MySQL 启动服务报错解决方案
    20亿与20亿表关联优化方法(超级大表与超级大表join优化方法)
    50行python代码实现个代理server(你懂的)
    nginx+tomcat反复请求
    慢慢过渡到个人博客
  • 原文地址:https://www.cnblogs.com/Template/p/9172072.html
Copyright © 2011-2022 走看看