18

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 Answers 2

21

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.

0
2

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.

2
  • 1
    to clarify, if /etc/cron.daily/logrotate exists, logrotate will run daily , perhaps at midnight?
    – majorgear
    Commented Nov 27, 2021 at 23:41
  • yes. default behaviour is to logrotate at midnight. Commented Mar 6 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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