Does logrotate depend on cron?
Asked Answered
A

2

16

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?

Albritton answered 12/11, 2014 at 13:17 Comment(0)
V
20

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.

Voltaic answered 3/9, 2018 at 8:21 Comment(0)
F
1

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.

Frailty answered 15/12, 2014 at 12:11 Comment(2)
to clarify, if /etc/cron.daily/logrotate exists, logrotate will run daily , perhaps at midnight?Cedar
yes. default behaviour is to logrotate at midnight.Bergson

© 2022 - 2024 — McMap. All rights reserved.