gpt4 book ai didi

bash - 使用 Bash 测试 URL 列表

转载 作者:行者123 更新时间:2023-11-29 09:46:05 25 4
gpt4 key购买 nike

我对cron命令了解有限,我曾尝试使用一个特定的 url,但我希望脚本能够获取逗号分隔的 URL 列表,并在 30 分钟内每 5 分钟检查一次它们的 http 响应。然后输出结果。我的猜测是:

5 * * * * /usr/bin/wget "www.example.com" --timeout 30 -O - 2>/dev/null  | \
grep "Normal operation string" || echo "The site is down" | \
/usr/bin/mail -v -s "Site is down" your@e-mail.address

但它只适用于一个特定的预定义网站

最佳答案

你宁愿把它放在一条线上来限制自己。只需将您的命令放入脚本中,将其作为可执行文件保存在某处,然后从 cron 文件中调用该脚本。

要获取多个站点,只需将它们设置在一个数组中,然后对其进行迭代:

#!/bin/bash
declare -a sites
sites=("www.example.com" "www.example.org" "www.example.net")
for site in "${sites[@]}"; do
if ! wget "$site" --timeout 30 -O - 2> /dev/null | grep "Normal operation string"; then
echo "The site is down" | mail -v -s "Site is down" email@example.com
fi
done

或者您可以从文件中读取它们。假设它们存储在 sites.txt 中,每行一个站点:

#!/bin/bash
while read -r site; do
if ! wget "$site" --timeout 30 -O - 2> /dev/null | grep "Normal operation string"; then
echo "The site is down" | mail -v -s "Site is down" email@example.com
fi
done < sites.txt

关于bash - 使用 Bash 测试 URL 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44289583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com