BASH- Hold script until all contents of a file is written

I have to hit a very large database to pull fields of information.

I have a script that runs multiple instance of the same query against the data base and writes contents to a file.

The script terminates before the file is completely written to confirmed by

ps -ef | grep <script name> 

The code below will hold the script until the file is created.
But I also want to know when writting has completed.

I guess my question is how do I look for EOF ?

while true 
do 
 [ -f resultfile ] && break
 sleep 2 
done 

Thanks alot in advance !

Perhaps you could try using either lsof or fuser to see when whatever process is writing to the file has ceased.

It is the application's job to tell you when it is done. It's very difficult and unreliable to try and detect the other way.

If you can't get the first applicaiton to manipulate a lockfile or otherwise signal your script, you can use incron (a cron like utility for file system events) to signal your second script that it is safe to read.

I use incron to detect the close file event for any file in my public ftp submission directory and as soon as it is done being uploaded, incron launches a script that yanks it away to a non-public directory so that every individual's uploads are private to other users whithout giving everyone a different area.

I also have a web interface to reboot my server, turn on and off anonymous FTP, share or not share my media directories (on/off or for a timeout period), etc. I know very little CGI, so instead of handling it in CGI, I save the state of the user radio button clicks and password hash to a text file and use Incron to detect a change and launch a shell script which parses and interperates it. I did it out of ignorance of CGI but I believe it is very secure.

Mike

1 Like