There are several ways to achieve this:
If you can, start the process from your script. The script will continue when the process ends, which means that it cannot write more data to the file.
If you cannot control the process, but know that the process ends after writing the file, you can find out the process identifier, and then check whether the process continues to work with kill -0 $PID
. If $?
- 0
, then the process is still alive.
If this is not possible, you can use lsof -np $PID
to get a list of all open files for this process and check if your file is in the list. This is somewhat slower.
[EDIT] Note that all of these approaches are somewhat fragile. The correct solution is for the external process to write the file using a temporary name and then rename it as soon as it is done.
Renaming ensures that everyone else either sees the entire file with all the data, or nothing.
source share