4

I am taking over administration for a server which has a custom made 3rd party Rails app. The app developer informed me that the ruby log files are getting large and pointed me to the following link: - https://stackoverflow.com/questions/4883891/ruby-on-rails-production-log-rotation

The logs are located in /root/production/app/log. This folder contains several files which end in .log. The current log files are about 2GB in size and the folder also has archived log files with the format of "logname.log.2013_01_18.bz2".

I tried searching the ssh command history to see if I can see which command was used to create the archive files, but the commands don't go back that far. I also ran "cat /var/lib/logrotate/status" but it doesn't look like logrotate did not rotate any logs from the above folder.

Basically:

  1. I would like to be able to run a command to rotate the logs manually and/or have the logs rotate automatically every Sunday night.
  2. If I need to shutdown my rails app to rotate the logs, I have no problem with this as I have a maintenance window every Sunday night to perform this.
  3. Finally, I am not sure if I should compress the log files weekly into bz2 or only compress the logs on a monthly basis for any rotated logs related to the previous month. This is mainly because I am not sure exactly how this app utilizes the logs and I have never utilized rails apps nor have I had to configure log rotation manually.

At this moment I do need to keep all log files and not discard any. Any information on concerns related to rotating logs, such as backing up logs before attempting to rotate are welcome.

1 Answer 1

12

logrotate is used by the system to rotate logs so you have 2 choices. You can either incorporate the rotation of these app logs into the systems rotations or setup your own and either run them manually or from the root user's crontab (Assuming the Rails app is run as root given it's directory is /root/...).

System rotation

To setup a logrotation within the system's pre-existing ones simply add a new file to the directory /etc/logrotate.d. Call it railsapp.conf. I'd use the other examples there to construct it. Also confrere with the logrotate man page.

User rotation

If you want to run your own instance of logrotate you only have to provide it with command line switches to do so.

  1. First make a copy of /etc/logrotate.conf /root/rails_logrotate.conf
  2. Edit the file so that it has the log rotation configured the way you want (i.e. keep all logs, rotate weekly, etc.)
  3. Run it

    # 1st time
    $ logrotate -d -f -s $HOME/my_logrotate.state logrotate.conf
    
    # afterwards
    $ logrotate -d -s $HOME/my_logrotate.state logrotate.conf
    

    If things look OK you can re-run these commands without the -d switch. This is for debugging purposes only and won't actually do any of the tasks, merely show you what it WOULD do.

    $ logrotate -s $HOME/my_logrotate.state logrotate.conf     
    

    You could also use the -v switch to make it verbose, similar to the output seen when using the -d switch.

Example

  1. Start with this log file.

    $ dd if=/dev/zero of=afile bs=1k count=10k
    10240+0 records in
    10240+0 records out
    10485760 bytes (10 MB) copied, 0.0702393 s, 149 MB/s
    
    $ ll afile
    -rw-rw-r-- 1 saml saml 10485760 Aug  6 14:37 afile
    
    $ touch -t 201307010101 afile
    $ ll afile
    -rw-rw-r-- 1 saml saml 10485760 Jul  1 01:01 afile
    
  2. Now run logrotate

    $ logrotate -v -f -s $HOME/my_logrotate.state logrotate.conf
    reading config file logrotate.conf
    reading config info for /home/saml/afile 
    
    Handling 1 logs
    
    rotating pattern: /home/saml/afile  forced from command line (1 rotations)
    empty log files are rotated, old logs are removed
    considering log /home/saml/afile
      log needs rotating
    rotating log /home/saml/afile, log->rotateCount is 1
    dateext suffix '-20130806'
    glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
    glob finding old rotated logs failed
    renaming /home/saml/afile to /home/saml/afile-20130806
    creating new /home/saml/afile mode = 0664 uid = 500 gid = 501
    
  3. Check the results

    $ ll afile*
    -rw-rw-r-- 1 saml saml        0 Aug  6 14:40 afile
    -rw-rw-r-- 1 saml saml 10485760 Jul  1 01:01 afile-20130806
    

Weekly Cron

To make this run every Sunday you could create the following crontab entry for the root user.

$ crontab -e

Add the following lines:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 0 * * sun logrotate -v -f -s $HOME/my_logrotate.state $HOME/logrotate.conf

Then save the above.

You can also use these types of shortcuts instead of specifying the actual days, mins, secs, etc.

string         meaning
------         -------
@reboot        Run once, at startup.
@yearly        Run once a year, "0 0 1 1 *".
@annually      (same as @yearly)
@monthly       Run once a month, "0 0 1 * *".
@weekly        Run once a week, "0 0 * * 0".
@daily         Run once a day, "0 0 * * *".
@midnight      (same as @daily)
@hourly        Run once an hour, "0 * * * *".

References

7
  • Thank you for the reply. I read that link before this post but some aspects confused me. With you example, I was under the impression that the -d switch was for debugging and caused the log to not actually rotate or be touched. Am I misunderstanding the switch? Also, do you know if there is a way to make the log rotate at a specific day/time like every sunday at midnight? I have to wait until after work hours to test this out.
    – Damainman
    Aug 6, 2013 at 18:15
  • @Damainman - see updates. If you need it at specific times then create a cronjob for it. I'll add that to the answer too.
    – slm
    Aug 6, 2013 at 18:43
  • Awesome, I will test this tonight and update this question. Thank you again!
    – Damainman
    Aug 6, 2013 at 18:52
  • My apologies and I believe this is due to my lack of knowledge, but I do not quite understand why rails_logrotate.conf was created but not called in your command "$HOME/my_logrotate.state logrotate.conf". What does my_logrotate.state do? Also for the user rotation, can I create the rails_logrotate.conf in /etc/logrotate.d , don't specific a time/criteria to run and just run it manually when needed?
    – Damainman
    Aug 7, 2013 at 9:03
  • I figured it out with your examples :). The -s and my_logrotate.state is only needed if using a different log state file. Also instead of using logrotate.conf from your user rotatation example, I instead used /root/mytools/rails_logrotate.conf. Thank you again!
    – Damainman
    Aug 7, 2013 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .