Does logrotate depend on cron?

26.8k views Asked by At

in the logrotate manpage, they say:

"Normally, logrotate is run as a daily cron job".

Does that mean that logrotate uses cron (or is executed by cron)? If so, does that mean that if I don't configure a cron job via crontab (for instance), logrotate will not work?

2

There are 2 answers

2
João Luís On

Yes, normaly, cron executes logrotate on a daily base. It depends on your linux distro, but the normal is to have cron running it.

You can check if have the file /etc/cron.daily/logrotate If thats the case, you distro uses cron to run logrotate.

If you are using docker, this can bring some problem, currently cron doesn't run inside a container.

0
Victor Wong On

You CAN run logrotate manually WITHOUT cron.

logrotate <configuration file>

However if you want to run logrotate on a scheduled basis, you will need a scheduling service like cron or a systemd timer.

Your package manager should create a default schedule in /etc/cron.daily/logrotate that runs logrotate with the default /etc/logrotate.conf configuration. You can also place your custom configurations in /etc/logrotate.d/ since the default configuration has a line that include all configurations in this directory.

include /etc/logrotate.d

If you want to run logrotate with a custom schedule, you can place your cron job in /etc/cron.d/.

For example, this would trigger logrotate using /etc/custom-logrotate.conf configuration every day at two o'clock.

0 2 * * * root /usr/sbin/logrotate /etc/custom-logrotate.conf

Checkout crontab guru if you need help with cron expression.

Newer distributions of Linux have systemd and utilise logrotate.timer service which by default runs logrotate daily at 00:00. /etc/cron.daily/logrotate has this at the top, skipping cron in favor of systemd timer:

# skip in favour of systemd timer
if [ -d /run/systemd/system ]; then
    exit 0
fi

In both cases, /etc/logrotate.d is by default monitored to be run daily.

Note that it doesn't matter how many times you invoke logrotate if the config /etc/custom-logrotate.conf specifies a frequency smaller than the times you're invoking. Your log will rotate only when the config file's frequency requirement is met. For example, if cron is invoking logrotate every 3 hours and your config file has daily frequency set. Your logs will only rotate after a day.

"Does that mean that logrotate uses cron (or is executed by cron)? If so, does that mean that if I don't configure a cron job via crontab (for instance), logrotate will not work?" - No, logrotate is a separate executable. It can still work if you put your conf file in /etc/logrotate.d (runs daily) or create a systemd timer or just run it manually.