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/)
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.
First make a copy of /etc/logrotate.conf /root/rails_logrotate.conf
Edit the file so that it has the log rotation configured the way you want
(i.e. keep all logs, rotate weekly, etc.)
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 alright 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
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
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
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;
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 * * * *".
I did this a while ago, I thiink my way COULD be wrong. But we all figure out
how to, because what I googled confused me, so I needed to figure it out on
my own. You can do it your own way, I guess.
If you like, a reference:
http://www.thegeekstuff.com/2010/07/logrotate-examples/