shawnmelliott has asked for the wisdom of the Perl Monks concerning the following question:

This is my first post so please feel free to flame for some bad etiquette if I make such a mistake. My question is this. I have a log rotation utility that copies logfiles to a backup directory and then zero's them out. One of the applications this is for creates a new set of logfiles each time it starts so it's not like they are always the same name. My problem then is this. I end up having hundreds of files in the original logfile directory that are not used and are zeroed out. I would just delete them but the application that creates the log files doesn't necessarily write to the ones it creates and thus they might have a byte size of 0 as well. Now... if I delete the 'active' log files that it uses it does not crash but it doesn't create the file again either and the I might need the information it would have logged if the file was there. I need a way to find out if a file is already opened by another application or not.

Replies are listed 'Best First'.
Re: is File Opened or Not
by Anonymous Monk on Jul 03, 2001 at 22:37 UTC
    You could have the program that writes the logs lock them and then check to see if the file is locked (remember to | LOCK_NB so the flock call won't hang). Alternately, if you are on a unix machine, you could parse the output of lsof to see if a file is open.
Re: is File Opened or Not
by tachyon (Chancellor) on Jul 03, 2001 at 23:23 UTC

    There seem to be a number of ways to skin this cat. Some possibilities would include:

    1. a cron job that deletes all zero byte files which have not been modified for x hours/days/weeks

    2. flock()ing your files so that ones in use can not be unlinked. This requires you to add code to all the applications that access the files.

    3. If you use 'open FILE, ">>log"' then log is created if it does not exist so I really don't understand what you mean when you say you only create them once. You can create them on the fly, on an as needed basis. You would thus not create the problem files in the first place.

    tachyon

    s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: is File Opened or Not
by bluto (Curate) on Jul 03, 2001 at 23:26 UTC
    Here are some suggestions, all of which have shortcommings. You'll need to decide if there are special cases that will keep these from working, so YMMV.

    You could backup and remove all files at system startup if you know you'll get to them before the application starts up.

    You could delete all zero sized files that were older than the last boot time of the machine (or just some really old date if you can't get the boot time and can live with an occasional missing log file).

    If the files have a PID in the name, common on Unix systems, you could parse out the PID, and then see if the process still exists with the same PID, and if not remove the file.

    bluto

Re: is File Opened or Not
by shawnmelliott (Initiate) on Jul 06, 2001 at 00:34 UTC
    I appreciate the quick response on this topic. The problem with the log issue is that it is for an application that I did not write. Thus I can't keep track of which files are old logs and which ones are not. I'm going to look into each of these suggestions and see if they'll work.

    Shawn