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


Hello,

I'm trying to figure out a way to use a Perl script to modify a log file that is being written to by a production web server. Specifically, I need a way to extract lines with the previous day's date from the log without shutting down the server or causing it to lose it's open file handle to the log file. Currently I use the following on the command line successfully:
grep 19/May access > access.030519;
grep -v 19/May access > temp.access; cat temp.access > access;
The data for the current day, 20/May, keeps getting written to the newly overwritten access file. I'm guessing this works because the cat temp.access>access occurs so quickly that it grabs the inode just released, which is the same inode the access file was using before the redirect overwrote it. Anyway, I would like to automate this process.
My question, is there a way to do this more safely in Perl? Should I just do the same thing in Perl using system()?

Thanks for any suggestions.
  • Comment on Modifying a log(text) file being written to by another process.

Replies are listed 'Best First'.
Re: Modifying a log(text) file being written to by another process.
by pzbagel (Chaplain) on May 23, 2003 at 21:37 UTC

    What is happening is that you are writing to the same file that the webserver has an open file handle for. AFAIK, the inodes do not change, you are clobbering the same file while it is opened by the webserver. This is dangerous and poorly designed. What you want is log rotation. Most webservers provide a method to rotate the logs. Apache has apachectl graceful. I recommend that you look into this method to rather than clobbering open files out from under your web server processes.

    HTH

      Thanks for your reply.
      Unfortunately, the reason I'm doing this at all is because the log rotation process fails randomly and without explanation for our IPlanet servers (using ns-cron). This process I described is only being used when normal log rotation fails for a particular instance.
Re: Modifying a log(text) file being written to by another process.
by little (Curate) on May 24, 2003 at 12:28 UTC

    man logrotate

    Have a nice day
    All decision is left to your taste