Today i will discuss about the shell script code snippet which will run for x hours.
Below is the code snippet which will help you to run code for x hours.
## Here I am using 10 seconds , in order to run for 1 hours, mention the same in seconds which is 3600. $vartime value will be set to 3600 initially.
vartime=$((SECONDS+10))
echo $vartime
## $SECONDS tells you the time period for which script is running.Initially it will be 0 seconds when script starts. So, 0 will be compared ##with 3600 in while loop.
while [ $SECONDS -le $vartime ]
do
## Write your Logic Here
Statements …..
## Below Logic is optional. Here this code is added if you want to exit from the Loop before the Loop ends with failure response.
if [ $SECONDS -ge $vartime ]
then
exit
fi
## Assume your logic took 2 seconds and then sleep of 2 seconds again. So, $SECONDS value will now become 4 seconds. In while Loop, 4 will be compared with ##3600.
sleep 2s
Done