This is one thorny problem. Take, for example, a file arriving via an ftp or scp delivery. The client program may open the file start writing, pause, write some more, operate slowly due to the bandwidth and eventually, maybe sometime finish the transfer. In the meantime your process, running quickly on the local box how no way of knowing whether it is safe to operate on the file.
Grrrrr there's no cross platform, consistent way to find out if a file is open. If you're in this position, you have to develop some sanity check method(s) to assure yourself the process writing to the file is finished.
If you're beginning to think I've been bitten by this, you're right. What we eventually did was:
- grep the output from lsof for the filename in question - this is handy if you're on a Linux system, useless anywhere else
- once the file doesn't appear in losf compare the atime to NOW. So when (time - (stat $filename)[8]) > $defined_comfort_level you can proceed under the semi-reasonable illusion that all is fine
At least the above method has worked for us for over two years now.
I wish there was a better way. If there is one I'd love to hear about it. If there is a filesysystem that implements something like electricians locks I'd put it on all the systems I'm responsible for.
Be Appropriate && Follow Your Curiosity
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.