This looks like a system design fault that cannot be resolved by technical means alone. If the idea is to maximise the survival time of the logging in a hostile environment then perhops the only way would be to reopen the file per log entry. If it's just a question of something else doing the archiving off, then this is definitely only resolvable by redesigning the logging and archiving so that they are aware of each other's needs - in that case flock would indeed help or perhaps combining functionality into the same process.