Bash while loop to run command for specific time with examples

How to run a shell script for 5 minutes in a bash while loop? linux sleep. bash wait. bash for loop. bash script for loop. for loop in shell script. bash sleep. script timer. How to loop a function to run for certain minutes and then exit? How to run a command in a shell script for few minutes and exit? Script Timer condition while loop in a shell. while loop a function and sleep to run for 15 minutes. shell script to run a command every 5 minutes using bash while loop. bash while duration. shell for loop. run bash script timer.

How to have a bash script run while loop until a specific time?

 

Earlier I had written an article on shell scripting interview questions along with their answers. In this article I will show some examples to run a function or command for specific time using bash while loop.

 

bash while loop for 5 minutes (define sleep duration as 30 seconds)

Here I have created a small script which will run for 5 minutes, and will run a command every 10 seconds. You can increase the Linux sleep timer from 10 seconds to any value for which you wish the script to wait before running the same command again. To wait for 1 minute you can use "sleep 1m"

Change the runtime variable value if you wish to change the total script timer for which the loop should run. So if you wish to run the while loop for 10 minutes then put runtime="10 minute"

# cat /tmp/testscript.sh
#!/bin/bash

runtime="5 minute"
endtime=$(date -ud "$runtime" +%s)

while [[ $(date -u +%s) -le $endtime ]]
do
    echo "Time Now: `date +%H:%M:%S`"
    echo "Sleeping for 10 seconds"
    sleep 10
done

Running our script. I have trimmed the output.

# /tmp/testscript.sh
Time Now: 23:56:26
Sleeping for 10 seconds
Time Now: 23:56:36
Sleeping for 10 seconds
Time Now: 23:56:46
Sleeping for 10 seconds
Time Now: 23:56:56
Sleeping for 10 seconds
Time Now: 23:57:06
Sleeping for 10 seconds

<<Output trimmed>>

Time Now: 00:00:56
Sleeping for 10 seconds
Time Now: 00:01:06
Sleeping for 10 seconds
Time Now: 00:01:16
Sleeping for 10 seconds
Time Now: 00:01:26
Sleeping for 10 seconds

 

bash while loop for 5 minutes (define sleep duration 1 minute)

Here we have kept our Linux sleep script timer as 1 minute so the date command will run every minute until 5 minute completes. You can replace the date command with any other command which you wish to execute.

# cat /tmp/testscript.sh
#!/bin/bash

runtime="5 minute"
endtime=$(date -ud "$runtime" +%s)

while [[ $(date -u +%s) -le $endtime ]]
do
    echo "Time Now: `date +%H:%M:%S`"
    echo "Sleeping for 1 minute"
    sleep 1m
done

Running our script

# /home/oamsys/testscript.sh
Time Now: 23:58:43
Sleeping for 1 minute
Time Now: 23:59:43
Sleeping for 1 minute
Time Now: 00:00:43
Sleeping for 1 minute
Time Now: 00:01:43
Sleeping for 1 minute
Time Now: 00:02:43
Sleeping for 1 minute
Time Now: 00:03:43
Sleeping for 1 minute

 

bash while loop for 5 minutes (define sleep duration 10 seconds)

In this sample script I will use a different logic to run my command for 5 minutes for every 10 seconds. Now you can tweak the script and modify the Linux sleep script timer for the time period you wish to keep as interval before running the command.

# cat /tmp/testscript.sh
#!/bin/bash

wait_period=0

while true
do
    echo "Time Now: `date +%H:%M:%S`"
    echo "Sleeping for 10 seconds"
    # Here 300 is 300 seconds i.e. 5 minutes * 60 = 300 sec
    wait_period=$(($wait_period+10))
    if [ $wait_period -gt 300 ];then
       echo "The script successfully ran for 5 minutes, exiting now.."
       break
    else
       sleep 10
    fi
done

Running the sample script

# /tmp/testscript.sh
Time Now: 00:27:26
Sleeping for 10 seconds
Time Now: 00:27:36
Sleeping for 10 seconds
Time Now: 00:27:46
Sleeping for 10 seconds
Time Now: 00:27:56
Sleeping for 10 seconds
Time Now: 00:28:06
Sleeping for 10 seconds
Time Now: 00:28:16
Sleeping for 10 seconds

<<output trimmed>>

Sleeping for 10 seconds
Time Now: 00:31:36
Sleeping for 10 seconds
Time Now: 00:31:46
Sleeping for 10 seconds
Time Now: 00:31:56
Sleeping for 10 seconds
Time Now: 00:32:06
Sleeping for 10 seconds
Time Now: 00:32:16
Sleeping for 10 seconds
Time Now: 00:32:26
Sleeping for 10 seconds
The script successfully ran for 5 minutes, exiting now..

 

Lastly I hope the steps from the article to have a bash script run while loop until a specific time on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.