我试图同时运行一个curl命令,并且本质上是运行一个计时器来计算它完成所需的时间。我们在 URL 的响应时间方面遇到了一些问题,我想创建一个计时器,如果超过 90 秒,它将重新运行卷发最多 2 次。第三次之后,它只会回显错误消息并退出。
我已经在 if 和 while 语句中尝试了类似于下面代码的许多变体,但是我得到了一个无限循环,我似乎无法在控制台中打破它,或者我让它跳到最后一个 if 语句:if [ $timer -eq 90] ; then
...,或者它根本不执行 if/elif 的任何部分。
这是我当前的代码:
retry=0
curl -K $conf/appdCurlConfig $prodc $base1d $base3d $base1w $base2w -o $prodCurl -o $base1dCurl -o $base3dCurl -o $base1wCurl -o $base2wCurl && cpid=`ps -o etime= -p $!`
SECONDS=0
timer=$(( cpid+$SECONDS ))
if [ $retry -lt 3 ] ; then
if [ $timer -eq 45 ] ; then
echo -e "\e[93mYour request is taking longer than expected, but is still processing\e[m"
fi
if [ $timer -eq 55 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -eq 65 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -eq 75 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -eq 85 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -ge 90 ] ; then
echo -e "\e[31mWe are experiencing some technical difficulty, or it has taken over 90 seconds to reach $appset; restarting your request\e[m"
run $param1 $param2 $param3
let retry++
else
if [ $retry -eq 3 ] ; then
echo -e "\e[93mWe are unable to reach $appset at this time, please try again in 5 minutes"
echo -e "If you keep getting this error message, please contact the system administrator\e[m"
exit 2
fi
fi
fi
我还尝试使用单个在后台运行它,我尝试将下面的内容制作成自己的函数并使用and&
调用它,并且我尝试将下面的内容包装在 中,所以它将是or 。&
&&
$(below code)
& $(code)
&& $(code)
ctimer() {
cpid=$(ps -o etime= -p $!)
SECONDS=0
timer=$(( cpid+$SECONDS ))
if [ $retry -lt 3 ] ; then
if [ $timer -eq 45 ] ; then
echo -e "\e[93mYour request is taking longer than expected, but is still processing\e[m"
fi
if [ $timer -eq 55 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -eq 65 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -eq 75 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -eq 85 ] ; then
echo -e "\e[93mYour request is still processing\e[m"
fi
if [ $timer -ge 90 ] ; then
echo -e "\e[31mWe are experiencing some technical difficulty, or it has taken over 90 seconds to reach $appset; restarting your request\e[m"
run $param1 $param2 $param3
let retry++
else
if [ $retry -eq 3 ] ; then
echo -e "\e[93mWe are unable to reach $appset at this time, please try again in 5 minutes"
echo -e "If you keep getting this error message, please contact the system administrator\e[m"
exit 2
fi
fi
fi
}
澄清一些变量,一个$conf/
是路径变量,$prodc
所有$base
*都是URL变量,其他的应该是不言自明的,$appset
是curl的内部应用。run
是此脚本中的一个函数,$param
* 是用户的初始输入。
我错过了什么,或者这是不可能的?kill
在再次尝试运行卷发之前,我是否还应该进行一次通话?感谢您的帮助。
答案1
您想得太多了,并且可能不知道某些内置功能bash
(由于您没有指定,我将假设您的 shell 是)提供的:
retries=0
timeout=90
duration=0
complete=0
maxretries=3
while [[ 0 -eq "$complete" ]]; do
curl -K $conf/appdCurlConfig $prodc $base1d $base3d $base1w $base2w -o $prodCurl -o $base1dCurl -o $base3dCurl -o $base1wCurl -o $base2wCurl &
curlpid=$! # capture PID of curl command
while [[ "$timeout" -gt "$duration" ]] && kill -0 $curlpid 2> /dev/null; do
sleep 1
duration=$((duration+1))
case $duration in
3)
echo "It's taking a bit longer.."
;;
30|45|75)
echo "It's taking a real long time but we'll keep waiting"
;;
85)
echo "We're about to give up"
;;
$timeout)
echo "We're giving up."
kill -TERM $curlpid
retries=$((retries+1))
if [[ "$retries" -ge "$maxretries" ]]; then
complete=1
fi
;;
esac
done
if wait $curlpid; then
complete=1 # curl returned non-error; we're done!
fi
done
kill -0
将发送一个空信号;它可以用来查看进程是否确实存在而不实际影响它。 shell 会将后台任务的 PID 捕获到$!
.你的if..elif
梯子是一个教科书式的例子,说明了一些东西可以折叠成一个case..esac
陈述。
答案2
如果您可以接受进度信息并不那么漂亮:
parallel --retries 3 --timeout 10 curl ::: url &&
echo success ||
echo failed
例子:
$ parallel --retries 3 --timeout 10 curl ::: 10.1.1.1 &&
echo success ||
echo failed
parallel: Warning: This job was killed because it timed out:
parallel: Warning: curl 10.1.1.1
parallel: Warning: This job was killed because it timed out:
parallel: Warning: curl 10.1.1.1
parallel: Warning: This job was killed because it timed out:
parallel: Warning: curl 10.1.1.1
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:10 --:--:-- 0failed