Ubuntu – Rotating a log file in the home directory on Ubuntu Server 14.04 LTS

loglogging

I have a log file ircd.log in /home/irc.

I want to rotate it daily at 00:00 and keep 7 days.

I want the rotated log files to be saved as ircd.log.YYYYMMDD for example ircd.log.20140505.

Can someone please tell me the package to use and also an example configuration for it based on this? Also is it possible to store this configuration at the user level?

Best Answer

As @bain mentioned, the package is called logrotate. To rotate this file as you have mentioned, you could use the following configuration in /etc/logrotate.d/irc:

/home/irc/ircd.log {
    daily
    rotate 7
    compress
    missingok
    create 0644 irc irc
    su irc irc
}

A breakdown of the configuration options:

  • The daily option tells logrotate to process this file every day when it is run.
  • The rotate 7 option tells it to keep 7 copies.
  • The compress option tells it to gzip the old files.
  • missingok tells logrotate not to complain if it is missing.
  • create tells the mode, owner, and group to use when creating files.
  • su changes the user who runs the compression, etc, so you may not want to include it
Related Question