0

I'm using Ubuntu Linux 14.04 I'm having trouble with log rotation. I have this file

rails@myapp:~$ ls -al myapp/log/production.log
-rw-r--r-- 1 rails rails 4522482443 Jun  5 12:11 myapp/log/production.log

and I have this in my /etc/logrotate.conf file,

/home/rails/myapp/log {
        daily
        rotate 4
        compress
        delaycompress
        missingok
        notifempty
        create 644 root root
}

but my log is never rotated. I know this because I can see entries in the "myapp/log/production.log" file dated from back on May 8th. Every day the log just keep getting bigger. What else do I need to do to have my log rotated?

  • 1
    Did you restart logrotated after updating its configuration? – DopeGhoti Jun 5 '17 at 16:29
  • 3
    Or did you try /home/rails/myapp/log/*.log { ... to match your *.log files? – thrig Jun 5 '17 at 16:30
  • 1
    Also note that some applications will continue to write to the rotated file instead of the newly created log file. For these you need to use the postrotate...endscript directives to do whatever is needed. Sometimes it's sending the app a simple HUP signal, some have a reload command, and others need to be restarted. – Deathgrip Jun 5 '17 at 16:45
  • @Deathgrip, I have rebooted my machine many times since May, so where am I adding the "postrotate...endscript" directive to? Also is the "..." in there short for anything or do I just cut and paste that as is? – Dave Jun 5 '17 at 18:38
  • @Dave - See the logrotate(8) man page. You will see examples. You can also look at the /etc/logrotate.conf and any /etc/logrotate.d/* files for examples. You may not need it. Your primary issue is the one noted in the comment by @thrig above. – Deathgrip Jun 5 '17 at 18:52
1

First, most likely you are not running logrotate automatically (usually through cron daemon).

Second, you specify in your logrotate config file that you want to rotate the file /home/rails/myapp/log but in the ls output you show us the /home/rails/myapp/log/production.log file (I'm assuming ~rails -> /home/rails). And you also ask for the new empty log file to be created by logrotate to have the owner root:root, but again in the ls output the original file is property of rails:rails.

TL;DR; use this config file instead (and make sure logrotate is run by cron or similar from time to time):

/home/rails/myapp/log/production.log {
        daily
        rotate 4
        compress
        delaycompress
        missingok
        notifempty
        create 644 rails rails
}

Also note that you might need to use postrotate to restart your app to force it to stop writing to the file descriptor that points to the rotated file.

0

Try this.

/home/rails/myapp/log

{  
   su rails rails
   daily
   missingok
   compress
   notifempty
   rotate 12
   create
   delaycompress
   missingok
   }

Place it in /etc/logrotate.d/myapp

Very small explanaition. Logroatate is to be write in your $HOME, so the rotate for your myapp.log has to execute as rails.

su rails rails   

sorry more from manpage

su user group Rotate log files set under this user and group instead of using default user/group (usually root). user specifies the user name used for rotation and group specifies the group used for rotation. If the user/group you specify here does not have sufficient privilege to make files with the ownership you've specified in a create instruc‐ tion, it will cause an error.

create mode owner group, create owner group Immediately after rotation (before the postrotate script is run) the log file is created (with the same name as the log file just rotated). mode specifies the mode for the log file in octal (the same as chmod(2)), owner specifies the user name who will own the log file, and group specifies the group the log file will belong to. Any of the log file attributes may be omitted, in which case those attributes for the new file will use the same val‐ ues as the original log file for the omitted attributes. This option can be disabled using the nocreate option.

Sorry I can not explain better in english.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.