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. 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 Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe