Waiting for all files to finish writing before continuing
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Waiting for all files to finish writing before continuing
I am automating a certain process using Linux Bash. Let's say I submit 5 jobs to 5 separate nodes and they write certain output files. I want to wait for all of those output files to finish writing before moving on to the next process in my program. However, each job takes a different amount of time to finish. The current version of my "wait" code is below. Each filename includes an integer starting at one (i.e. job1, job2, job3, job4, job5, etc). $k is the last file in that list, in this example and code below, it would be equilibration5.check. Ok, so the problem is equilibration5.check could be be created before, let's say, equilibration3.check depending if equilibration5 got a faster node than equilibration3. Well, in my code it would move on to the next part before allowing equilibration3 to finish and write equilibration3.check. The next code snippet would then check all of the existing equilibration.check files for "DONE" and then move on to the next process, possibly before some of the other .check files exist.
So, what is the best way to make my program wait until all of the .check files exist, regardless of what order they finish?
FILE=~/$a/MinAnnealEquilMD/Restart_Junk/equilibration$k.check
while test ! -f $FILE
do
sleep 2;
done
until false | grep -q "DONE" ~/$a/MinAnnealEquilMD/Restart_Junk/equilibration*.check
do
sleep 2
done
There are several ways. The wait command, which is built-in, will pause until all processes finish before proceeding. If you combine that with functions, it could be structured like this:
The built-in wait command found in Bash has additional capabilities. See "man sh" and "man bash". Both manual pages are quite long so to maximize their utility, get comfortable navigating them and using the shortcuts available in the man utility.
Also the sync command. (Or the sync() system call.) This forces all pending disk-writes to be completed.
A simple way to handle this would be to write a program or script which "forks" a set of child processes to perform each task, then waits for all of them to finish. Each process ends with a "sync" call to make sure that all physical I/O is completed before it ends.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.