1

I have a 8GB file called php.log with a running php script logging into it. It is important for me that I log every event, and I want to compress it and empty the current file without stopping the web server.

If I run:

    mv php.log php.log.backup20140305-01
    touch php.log

I will lose some of the data. How can I do this without losing any data?

valiano
  • 508
  • 4
  • 10
Mohammad Jolani
  • 333
  • 1
  • 2
  • 8

2 Answers2

5

You'll find it easier to configure logrotate to do the rotation for you. If you create a file called /etc/logrotate.d/php containing something like the following, it'll handle the log rotation automatically. This is just a guide, so make sure to test and customise it before you put it into production.

/path/to/php.log {
    daily  
    missingok              # don't rotate if the file isn't there...
    notifempty             # ...or if it's zero-length
    rotate 30              # keep 30 days' worth of logs
    compress               # gzip the logs, but...
    delaycompress          # ...only after they're over a day old
    create 640 root adm    # permissions with which to create new files
    sharedscripts
    postrotate
        /etc/init.d/apache2 graceful    # or whatever makes your process let go of the log file
    endscript
}

NB: the comments in this extract break logrotate syntax, so make sure to strip them out of your live config file.

Flup
  • 7,688
  • 1
  • 31
  • 43
  • But does `logrotate` guarantees no log data is lost? From what I read, it isn't always the case. – valiano Aug 07 '18 at 11:41
0

You should be using logrotate as Flup has said, but a quick way of flattening/truncating a log-file without breaking current file handles is this:

> /path/to/php.log

Be aware that you will lose data by doing this, so you should only truncate if you are sure you have the data elsewhere, but it's a useful trick when you don't care that much about the contents of a log-file and you have an uninterruptable process writing to it.

Craig Watson
  • 9,370
  • 3
  • 30
  • 46