定时轮询访问网站脚本

LAMP2年前 (2022)发布 liyugang
56 0

node 方案
------------------------
const request = require('request'); // 引入 request 模块

const domainList = ['example.com', 'example.org', 'example.net']; // 指定域名列表
const interval = 5000; // 指定轮询间隔,单位为毫秒

function poll() {
    // 从域名列表中取出第一个域名
    const domain = domainList.shift();

    // 向域名发送请求
    request(`http://${domain}`, (error, response, body) => {
        if (error) {
            // 如果请求失败,打印错误信息
            console.log(`Error: ${error.message}`);
        } else {
            // 如果请求成功,打印响应结果
            console.log(`Response from ${domain}: ${body}`);
        }

        // 将域名插入域名列表的末尾
        domainList.push(domain);

        // 重新开启一个轮询
        setTimeout(poll, interval);
    });
}

// 开启第一个轮询
poll();




linux shell 方案
---------------
#!/bin/bash

# 域名列表
domainList=(
    "a.com"
    "b.com"
    "c.com"
    "d.com"
)

# 轮询间隔,单位为秒
interval=10800

function poll() {
    # 从域名列表中取出第一个域名
    domain=${domainList[0]}

    # 打开域名对应的网页
    wget "http://$domain"

    # 将域名插入域名列表的末尾
    domainList=(${domainList[@]:1} ${domain})

    # 重新开启一个轮询
    sleep $interval
    poll
}

# 开启第一个轮询
poll



win bat 方案
-----------------
@echo off

rem 域名列表
set domainList=a.com b.com c.com d.com

rem 轮询间隔,单位为秒
set interval=10800

rem 轮询函数
:poll

rem 从域名列表中取出第一个域名
for /f "tokens=1 delims= " %%d in "%domainList%" do set domain=%%d

rem 打开域名对应的网页
curl "http://%domain%"

rem 将域名插入域名列表的末尾
set domainList=%domainList: = % %domain%

rem 重新开启一个轮询
ping -n %interval% 127.0.0.1 > nul
goto poll

rem 开启第一个轮询
goto poll








#!/bin/bash

# 本脚本加入到定时运行中, 每20分钟访问一次
# */20 * * * * /bin/sh /home/www/tool/monitor.sh > /dev/null 2>&1

[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#define array url list
array=(
http://www.baidu.com/
http://www.163.com/
http://www.taobao.com/
)

check_url(){
  # wget -T 5 -t 2 --spider $1 &>/dev/null
  curl --connect-timeout 3 -sL -D- $1 > /dev/null
}
main(){
  for((i=0;i<${#array[@]};i++))
  do
    check_url ${array[i]}
  done
}
main
exit

© 版权声明

相关文章

暂无评论

暂无评论...