in reply to Logging to a filehandle of a deleted file

If you link the original file to a backup filename then even while another process deletes the file you can still view the original content plus your additions by using the backup filename. One could even restore the original file, see example below:
use strict; use warnings; my $filename = 'logfile.txt'; my $backupfile = 'logfile.backup'; open(my $handle, '>>', $filename) or die; link $filename, $backupfile; unlink($filename); print $handle "First line\n"; syswrite($handle, "Second line\n"); close($handle); rename $backupfile,$filename; # ok to fail if $filename still exists unlink $backupfile;
Note that while your filehandle remains open you can still write to the deleted file. Deleted files will not be visible in directory listings but the open handle would show up on the systems list of open handles (e.g. the command lsof in Lunix and Unix systems would reveal them, provided you have enough system privileges).
And yes, while you keep writing to that file it will occupy more and more space on disk, a little nightmare for inexperienced system administrators that see disk usage grow and do not know why.

Replies are listed 'Best First'.
Re^2: Logging to a filehandle of a deleted file
by Moron (Curate) on Feb 28, 2007 at 15:02 UTC
    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.

    -M

    Free your mind