I am looking for a solution I need to delete log files, but there might be a possibility that they are being accessed at the moment the delete call is made. By being accessed, I mean a process is either reading or writing to the file. In such cases, I need to skip the file instead of deleting it. Also my server is Linux and PHP is running on Apache.
What I am looking for is something similar to (in pseudo-code
):
<?php
$path = "path_to_log_file";
$log_file = "app.log";
if(!being_accessed($log_file))
{
unlink($path.$log_file);
}
?>
Now my question is how can I define being_accessed
? I know there might not be a language function do to this directly in PHP. I am thinking about using a combination of sections like last_access_time
(maybe?) and flock
(but this is useful only in those conditions where the file was flock
-ed by the accessing application)
Any suggestions/insights welcome...