78

I tried to set up a log rotation of logs files located at /tmp/hr_logs/. For setting up, I used logrotate in linux and I'm able to rotate it on daily basis using the following config in my /etc/logrotate.conf

  /tmp/hr_logs {
  daily
  rotate 4

With this config, file will rotate on daily basis and system will keep 4 copies of the log file appended with date [format: -YYYYMMDD]

Now, I trying to set up a different set of log files which I need to make rotate on hourly basis and for it, i have done the configuration in logrotate.conf:

  /tmp/last_logs {
  hourly
  rotate 4

But this is not at all working? Can anyone please guide me on this please?

scai
  • 20,297
  • 4
  • 56
  • 72
Kishore
  • 901
  • 1
  • 6
  • 4

2 Answers2

103

The manpage of logrotate.conf contains an important advice for the hourly option:

Log files are rotated every hour. Note that usually logrotate is configured to be run by cron daily. You have to change this configuration and run logrotate hourly to be able to really rotate logs hourly.

As pointed out by yellow1pl the solution is to copy the file /etc/cron.daily/logrotate into the /etc/cron.hourly/ directory. This works at least for Debian and possibly some Debian derivates.

scai
  • 20,297
  • 4
  • 56
  • 72
  • 3
    This also works in CentOS – EminezArtus Jun 12 '15 at 15:18
  • 10
    Copy or move the file? Also, shouldn't I change "daily" to "hourly" inside /etc/logrotate.conf? – Mario Mey Jul 31 '17 at 13:04
  • 3
    I would symlink as if the script gets updated you will inherit any changes. Also if you are using the dateext option you will need to reformat the extension so you can have multiple uniq filenames per day. I use the following. dateext dateformat -%Y%m%d-%s – krad Nov 28 '17 at 15:00
  • 5
    The command I used for symlink: `ln -s /etc/cron.daily/logrotate /etc/cron.hourly/logrotate` – Roland Pihlakas Aug 12 '20 at 17:53
  • This works with RedHat but at least with RH8 you need to execute `sudo semanage permissive -a logrotate_t` or otherwise `/etc/cron.hourly/logrotate` won't have permissions to edit the log files, no matter the daily version did. I didn't try a symbolic link instead. I don't know if there is a difference for SELinux. – aalku Oct 24 '22 at 10:42
  • Cron is the past: `systemctl edit --full logrotate.timer` – wick Dec 05 '22 at 19:01
26

There is /etc/cron.daily/logrotate script for daily logrotates. However there is no such script by default in /etc/cron.hourly/ directory. Copy this script and it should work fine.

rsm
  • 2,530
  • 4
  • 26
  • 33
  • 1
    As suggested, I have copied logrotate under /etc/cron.hourly but no luck. – Kishore Aug 26 '14 at 14:39
  • 1
    @Kishore did you also use the "hourly" option? Make sure it is supported on your distro by reading the manpage on your OS, `man logrotate` – f0ster Jul 18 '16 at 20:27
  • 21
    wouldn't you want to move `logrotate` rather than copy? otherwise there will be a time when logrotate runs twice at the same time – mulllhausen Feb 13 '17 at 07:36
  • 14
    I found I had to restart the `crond` service after moving `logrotate` to `/etc/cron.hourly`. E.g. in CentOS 7 `sudo systemctl restart crond` – cherdt Apr 19 '17 at 16:13
  • 4
    @mulllhausen `logrotate` locks the state file by default and exits if the lock is already taken - so no there's no issue with being in both the `cron.hourly` and `cron.daily` schedules even if they run at the same moment. – rvalue Sep 14 '21 at 23:42
  • Hi, I'm using Cent OS, even after adding cron.hourly. I still don't see it working. – Pankaj Rathi Mar 15 '22 at 09:03