I have a Java script that executes a k-shell which calls a program that writes file A. Thereafter, the same Java script parses output file A. No other process read or writes file A.
The problem is that file A is not always ready to be parsed in time, which I assume is because the file system is still finishing its job even after the program & calling shell return. This only occurs when there are a lot of processes running.
lsof does not seem to work on my system. If I open a file with an editor, lsof shows me the editor process, but if I pass the filename to lsof I get: lsof: no file use located: filename
I don't think a fileLock will work, as I would have to unlock it before either returning from the program or the k-shell, by which time the file still might not have been closed completely.
My thought was to write a short script that would rename (mv) the file, and then have the parser parse the renamed file, my understanding being that a file cannot be renamed until it is completely written. I put the mv command in a while loop that checks to see if the stdOut from the mv command has anything in it, theory being that if it is empty, we're good to go.
#! /bin/ksh
# Move a file. The idea is that this script will not return until the mv is
# complete. If mv is successful, mv.out.txt should be empty.
mv $1/camber.out $1/camber.out.copy > mv.out.txt
while [[ -s mv.out.txt ]] ;
do
echo "mv did not happen"
mv $1/camber.out $1/camber.out.rename > mv.out.txt
done
echo "mv should have taken place"
exit
I have yet to see an instance where the mv was held up. Any comments, suggestions, hints, or insults would be greatly appreciated. Thanks in advance to all.