I am trying to simultaneously run a curl command and, essentially, run a timer against how long it takes for it to complete. We have been having some issues with the URLs' response time and I would like to create a timer that will rerun the curls up to 2 more times if it goes beyond 90 seconds. After the 3rd time, it would just echo an error message and exit.
I have tried many variations of something similar to the code below in if and while statements, but I get an endless loop that I cannot seem to break out of in the console, or I have had it just jump to the last if statement that says if [ $timer -eq 90] ; then
..., or it just doesn't do any part of the if/elif at all.
Here's my current code:
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
I have also tried running it in the background with a single &
, I have tried making below into its own function and calling it with both &
and &&
, and I've tried wrapping the below in $(below code)
, so it would be & $(code)
or && $(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
}
To clarify some of the variables, $conf/
is a path variable, $prodc
and all the $base
* are URL variables, the other should be self explanatory, and $appset
is the internal application to curl. run
is a function within this script and $param
* are the user's initial input.
What am I missing, or is it just not possible? Should I also include a kill
call before I try running the curls again? Thank you for your help.