linux - 一天中特定时间段之间的调用旋度

标签 linux bash shell curl

我必须使用curl执行一个URL,如果输出包含“hello”字符串,那么我将成功退出shell脚本,否则我会一直重试直到早上8点,然后退出并显示错误消息,如果它仍然不包含该字符串。

我得到了下面的脚本,但我无法理解如何运行 while 循环直到上午 8 点,并且如果仍然curl 输出不包含“hello”字符串?

#!/bin/bash

while true
do
    curl -s -m 2  "some_url" 2>&1 | grep "hello"
    sleep 15m
done

因此,如果是下午 3 点之后,则开始进行curl 调用,直到上午 8 点,如果该curl 调用成功并给出“hello”字符串,则成功退出,否则在上午 8 点后退出并显示错误消息。

如果是下午 3 点之前,那么它将继续休眠,直到下午 3 点过去。

我必须在脚本中添加此逻辑,并且我不能在此处使用 cron。

最佳答案

您可以按如下方式使用该脚本,已使用 GNU date 进行测试

#/bin/bash

retCode=0                                      # Initializing return code to of the piped commands
while [[ "$(date +"%T")" < '08:00:00' ]];      # loop from current time to next occurence of '08:00:00'
do
    curl -s -m 2  "some_url" 2>&1 | grep "hello" 
    retCode=$?                                 # Storing the return code
    [[ $retCode ]] && break                    # breaking the loop and exiting on success           
    sleep 15m                                  
done

[[ $retCode -eq 1 ]] && echo "String not found" >> /dev/stderr  # If the search string is not found till the last minute, print the error message

关于linux - 一天中特定时间段之间的调用旋度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39738491/

相关文章:

bash - 下载大于一定大小的文件

bash - 从 svn 获取分支创建日期

linux - 如何将数百个文件从多个子目录传递到 cat?

string - 在 bash 的字符串中“揭示”隐藏/控制 'codes'

bash - 在 make clean 上使用通配符时如何修复 Makefile 语法错误?

c - 从Linux内核模块访问/dev/mem

linux - 显示父目录中文件的内容

linux - RHEL5 上的 CVS 版本

linux - 可选择从标准输入或文件中读取的 bash 标志

shell - 在后台 shell 脚本中使用 netcat/cat(如何避免停止(tty 输入)?)