I have an external process that start a write to a file.
How do I write script that waits until the file is closed (when the other process done with the writing).
I have an external process that start a write to a file. How do
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are several ways to achieve this:
If you can, start the process from your script. The script will continue when the process terminates and that means it can’t write any more data to the file.
If you can’t control the process but you know that the process terminates after writing the file, you can find out the process ID and then check if the process is still running with
kill -0 $PID. If$?is0afterwards, the process is still alive.If that’s not possible, then you can use
lsof -np $PIDto get a list of all open files for this process and check if your file is in the list. This is somewhat slow, though.[EDIT] Note that all these approaches are somewhat brittle. The correct solution is to have the external process write the file using a temporary name and then rename it as soon as it’s done.
The rename makes sure that everyone else either sees the whole file with all the data or nothing.