Home BASH How To Repeat A Command Until It Succeeds In Linux

How To Repeat A Command Until It Succeeds In Linux

By sk
Published: Last Updated on 8.1K views

In this brief guide, we are going to learn how to repeat a command until it succeeds in Linux. This can be helpful in many occasions. For instance, you can use this method to verify network connectivity between two or more hosts or check Internet connectivity to see if you are still online or offline. So whatever the reason is, if you ever wanted to repeat a Linux command or program until it ends successfully, this guide will help.

Repeat Command Until It Succeeds in Linux

For repetitive execution of a list of commands, we use the following BASH looping constructs:

  1. While loop,
  2. Until loop.

While Loop executes the block of code (enclosed within do ... done) when the condition is true and keeps executing that till the condition becomes false. Once the condition becomes false, the while loop is terminated.

Until Loop is just opposite of While loop. It executes the block of code (enclosed within do ... done) when the condition is false and keep executing that till the condition becomes true. Once the condition becomes true, the until loop is terminated.

First, let us see how to repeat a Linux command or a program until it succeeds using While loop.

1. Repeat Commands Using While loop

Have a look the the following command:

$ while ! ping -c 3 ostechnix.com ; do sleep 2 ; done ; xcalc

This command will keep trying to ping ostechnix.com site. Once the site comes online, the While loop will end and display the "xcalc" program. Here, xcalc is optional. It is just for notification purpose. Otherwise, we may need to keep checking the Terminal window to verify whether the command is succeeded or not.

Explanation:

Let us break down the above command and see what each statement does.

while ! ping -c 3 ostechnix.com ;

This is the conditional statement. As you see in this statement, we prefaced the ping command with ! character. It serves as "NOT" condition. So the actual meaning of the above line is - while NOT able to ping ostechnix.com.

do sleep 2 ; done ;

The statement "do sleep 2", indicates that don't hammer the system by repeatedly trying the ping command every second. We instruct the system to wait for a bit, for example 2 seconds, and then try to ping ostechnix.com. In other words, the While command will keep pinging the site every 2 seconds. Once the condition becomes false, the while loop will terminate.

xcalc ;

Run xcalc program. This program will run only after the successful execution of the previous command.

So as per the above command, If the condition is true (i.e if ostechnix.com is "NOT" reachable), it will execute block of code enclosed in do...done. Here, the code is "sleep 2". So it waits for 2 seconds and then try again to ping the site. It will keep executing that till the condition becomes false. Once the condition becomes false (i.e if ostechnix.com is reachable), the loop will end and finally the xcalc program will open.

Here is the sample output of the above command:

ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
ping: ostechnix.com: Name or service not known
PING ostechnix.com (104.26.5.40) 56(84) bytes of data.
64 bytes from 104.26.5.40 (104.26.5.40): icmp_seq=1 ttl=55 time=63.6 ms
64 bytes from 104.26.5.40 (104.26.5.40): icmp_seq=2 ttl=55 time=84.8 ms
64 bytes from 104.26.5.40 (104.26.5.40): icmp_seq=3 ttl=55 time=79.9 ms

--- ostechnix.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 63.602/76.114/84.810/9.068 ms
Warning: Cannot convert string "-adobe-symbol-*-*-*-*-*-120-*-*-*-*-*-*" to type FontStruct

2. Repeat Commands Using Until loop

You can also do this using "Until" loop like below.

$ until ping -c 3 ostechnix.com ; do sleep 2 ; done ; xcalc

Let us breakdown this command and see what each statement does:

until ping -c 3 ostechnix.com ;

This is the conditional statement. It means - ping until the site is reachable.

do sleep 2 ; done ;

Try to ping the site every 2 seconds until it is reachable. Once the condition becomes true i.e. the site is reachable, the loop will be terminated.

xcalc

Run xcalc program. It will run only upon the successful execution of previous commands.

So as per the above command, the Until loop will keep pinging the ostechnix.com until it succeeds. Once the site is reachable, the until loop will end and the xcalc program will open.


Related read:


3. Repeat Previous Commands

We know now how to a Linux command until it exits successfully. What about the previous command? How do you repeat or retry the previous commands until they succeeds? That's easy too!

We already have posted different ways to repeat the last command. If you don't know how to do it, refer the following guide.

As stated in the above link, to execute any last executed command, just type double exclamation marks, and hit ENTER like below:

$ !!

This will execute the last command.

So if you want to repeat previous commands until they succeeds using While loop, run:

$ !!; while [ $? -ne 0 ]; do !!; done

To repeat previous commands until they succeeds using Until loop, run:

$ until !!; do :; done

For more examples on Bash While loop and Until loop, refer the following guide:

You May Also Like

2 comments

Aa micho September 23, 2021 - 3:19 pm

Good content but ur site have very bad design

Reply
sk September 23, 2021 - 9:54 pm

Thanks. And for the site design, can you be more specific? Which part you don’t like?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More