Delete file if it is not currently available

I am looking for the solution that I need to delete the log files, but there may be a chance that they are accessed the moment the delete call is made. When accessing, I mean that the process is reading or writing to a file. In such cases, I need to skip the file, not delete it. Also my Linux and PHP server runs on Apache.

What I'm looking for is like ( 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 to determine being_accessed? I know that in PHP there can be no language function. I am thinking of using a combination of partitions such as last_access_time(maybe?) AND flock(but this is only useful when the file was flock-ed from application to application)

Any suggestions / ideas are welcome ...

+5
2

, (, .. , lsof, , , linux/unix ( ), , . , , 1 , - . ( unlink() ), , , , ( ), , invisible (, , , , (.. df, , )) , , - . , , phy . , . unlink, , ( Windows), @ (@unlink()), ,

+6

( ):

<?php
$path = "path_to_log_file";
$log_file = "app.log"; 
@unlink($path.$log_file);

@, , , ( )

+1

All Articles