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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe