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 $*        #把所有命令行接收到的参数作为函数参数传给函数内部
  • 相关阅读:
    Struts2+Spring3+Mybatis3开发环境搭建
    spring+struts2+mybatis
    【LeetCode】Populating Next Right Pointers in Each Node
    【LeetCode】Remove Duplicates from Sorted Array
    【LeetCode】Remove Duplicates from Sorted Array II
    【LeetCode】Binary Tree Inorder Traversal
    【LeetCode】Merge Two Sorted Lists
    【LeetCode】Reverse Integer
    【LeetCode】Same Tree
    【LeetCode】Maximum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/Template/p/9172072.html
Copyright © 2011-2022 走看看