0

I've been searching online for days on how to set up my server to automatically rotate the logs for a Rails app my team recently released. I've gotten myself as far as being able to run sudo logrotate -f /etc/logrotate.conf but of course, who wants to do that all the time?

The contents of the config file for the app's log (I want to add more, but don't see a need to when I can't rotate one file yet):

/path/to/app/production.log {
  daily
  missingok
  rotate 7
  compress
  delaycompress
  notifempty
  copytruncate
}

I've verified the /etc/logrotate.conf file contains this line:

include /etc/logrotate.d

But this is the part where I'm not too sure where to go. I've found many different approaches at actually automating the process, but none seem to work. For the record, I've verified the server has the anacron command installed, but I don't know how to configure it for any process of my own. Also, root does not have a crontab on the server yet (we haven't needed it), and I'm unsure if that's better to use than /etc/crontab. In the /etc/crontab file, I've added:

15 0 * * *   root    cd / && run-parts --report /etc/cron.daily

but I've seen other people use

15 0 * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

Is the latter a better option? Why? If so, how do I ensure it works? Again, I don't know how to set up anacron for the task at hand.

Finally, here are the previous contents of the /etc/cron.daily/logrotate file:

/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
  /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

and after some research, I replaced that with this (which I understand a bit better):

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

Can someone explain to me what the first config was doing, and which of these two options is better? I'm unsure why I have to force this process just to get it to run. Maybe /etc/crontab doesn't work like I think it does?

Brad Rice
  • 1,334
  • 2
  • 17
  • 36

2 Answers2

1

Is the latter a better option? Why?

With your cron command, /etc/cron.daily/* will only ever run if the computer is on at midnight (00:15). If you turn it off at night, as some people do, it would never run.

The work around this, and instead run the command when the computer starts later in the day, one can use anacron. This is obviously less useful for servers than desktops.

Of course, you don't want to use both at once, since that would run the jobs twice a day. Therefore, cron, the most brittle mechanism, will yield to anacron by only running the job if anacron is not installed.

This is what Debian and Ubuntu does by default with their test -x /usr/sbin/anacron || crontab job prefixes.

All server distros will come with logrotate correctly set up, so you shouldn't be modifying the crontab, anacrontab, or /etc/cron.daily/logrotate. The only thing you should do is add a file to /etc/logrotate.d.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thanks for the explanation! My real head scratcher is why none of these logs seem to ever be rotated. I may have messed myself up by trying to force the rotations for the past few days. I don't know if it has to have been over 24 hours for the server to think the log needs rotating, but with the crontab including your `test -x /usr/sbin/anacron ||` command, the logs weren't rotated a couple days ago. I'll let her sit for a few days without touching anything and see if it works... – Brad Rice Feb 27 '13 at 01:04
1

Try putting this in /etc/crontab file : -*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1

It works for me.

Vijay
  • 41
  • 7