I need to write an Unix Shell Script tran.sh that moves the csv input files from /exp/files folder to /exp/ready directory. The csv input files are written to /exp/files folder by an FTP server whose behavior I cannot trivially change. In tran.sh shell script I need to ensure before doing a move of that csv input file from /exp/files directory no longer any other process is writing to the file. how can i do it.
feedback
|
migrated from stackoverflow.com Feb 23 '10 at 5:28
This question came from our site for professional and enthusiast programmers.
|
There is no portable way to do so. You can try | |||
feedback
|
|
Try using Here's some example code that will wait for a file to become ready to move:
| |||||||||||
feedback
|
|
you can use lsof
| |||||||
feedback
|
|
The Right Way to do this is to cause the process writing the files to and rename or move them of its own volition when done with the write. Anything else is prone to race conditions and/or permission problems. Some particular examples of problem cases:
Other, more subtle race conditions may exist as well -- and regardless, lsof, fuser and such are not POSIX tools and not available everywhere. Require as a matter of the protocol that the processes writing the files move them into the final location on completion themselves; it's the only safe and portable approach. EDIT: It has been clarified that the files are being written not by an arbitrary process (which may close and re-open them) but by an FTP server. In this case, incron can be used to run an arbitrary script whenever a file has been closed in this directory. | |||||||
feedback
|
|
I don't think you can do this without modifying processes that create these files. Whenever we have task like this in our systems we make sure that process that creates file make it executable at the very end. So process that will move or post-process some other way such files can check for executable bit to make sure originating process has finished working with a file. | |||
feedback
|