我必须使用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/