Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to repeat a Linux command until it succeeds

How-To
May 06, 20204 mins
Linux

You can easily set up a Linux command that keeps trying until it succeeds. Let's look at how to loop your way to success.

nautilus shell
Credit: Thinkstock

If you want to run a command on a Linux system until it succeeds, there are some really easy ways to do it that don’t require you to retype the command repeatedly or sit in front of your screen pressing !! (repeat the previous command) until the command works. In this post, we’ll look at two options available with bash.

Using while !

First, we’ll look at an easy example of trying to display the contents of a file. This trick is to use the bash while command to create a loop, but preface the command that you want to run with a ! sign so that it loops until the command succceeds. Here’s how it works.

$ while ! cat missingfile

The ! serves as a “not” condition. Read this line as “while NOT able to display the specified file”.  The attempt to display the contents of “missingfile” would fail if the file is actually missing or if, for some reason, you simply don’t have read permission to the file. To complete the while command, however, you then need to specify what should happen while the file is unavailable. So, here we add some lines to flesh out the while command.

$ while ! cat missingfile
do
    echo waiting for missingfile
    sleep 10
done

Once some person or process drops missingfile into place, the cat missingfile command should succeed (assuming you have permission to view it), and the while command will exit on its next check. The sleep command ensures that you don’t try many times every second, rapidly filling your screen up with “waiting for missingfile” messages. How often you check should depend on whether you want to see the file as soon as it’s available or wait for a bit – in this case, up to 10 seconds.

The echo command is really not needed unless you want to be reminded that you’re waiting for the file to be available.

Once the file is available, the while command will exit and you can move on to the next step – probably using the file in some way or moving on to some other task once you’re sure the file is available for later use.

You can also use this approach when running a script. In this command, we wait until the runme script runs successfully with the while command trying every 10 seconds.

$ while ! ./runme
do
    sleep 10
done

Note that the “while ! ./runme” command is the equivalent of specifying “while running the script fails”.

Using until

You can also use the until command to accomplish the same kind of thing. Instead of “while not” logic, you would use “until it is” logic. These commands will work the same as those shown above.

$ until cat missingfile
do
    echo waiting for missingfile
    sleep 10
done
$ until ./runme
do
    sleep 10
done

The loops will exit once missingfile appears or the runme script is successfully run.

Looping in scripts

You can also use the while ! or until commands to repeatedly try to run some command in scripts. If you have a script which depends on some other file to run, you could do something like this:

#!/bin/bash

until ls myscript 2>/dev/null
   echo waiting
   sleep 10
done
./myscript

The script will keep trying until it can run the specified script.

Notice that this script displays messages showing that it’s waiting for myscript to appear, but it sends its error output from the test to /dev/null (aka the “bit bucket”) to prevent the script from displaying errors that suggest something is broken. Once myscript is available, the loop exits and the script is run.

Of course, you might not even want to see the “waiting” messages filling up your screen. You can always omit the echo command altogether, especially once you’re confident that the script will eventually be run and that you’ll be notified when it completes.

Now see

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.