in reply to Looking for log files with open file handles in linux.

To avoid tromping on a file that is open for output elsewhere, as well as to avoid someone else tromping on your file, you can use flock. And to avoid race conditions, you open, flock, work, unlock, close. In other words, it's not safe to open, test the lock, close, and then later assume that you can re-open and work on the file. By then the file could be used elsewhere.

One issue that is not an issue is unlocking before closing. One might think that since close flushes the file handle one final time, any buffered output might get written after the unlock. Perl deals with this for you by flushing the filehandle upon unlocking. Be sure to read the docs on flock. Locking is tricky, and has to be done right.

Of course this is assuming that other processes are actually locking their logfiles, as they ought to. Well behaved programs will, but there's no guarantee that everything you're looking at is well behaved.


Dave

  • Comment on Re: Looking for log files with open file handles in linux.