how to run a logrotate hourly
Asked Answered
Z

4

6

I created a program.conf that logrotate my logs hourly in an EC2 instance. the logrotate works well when i force it command (by sudo logrotate program.conf --verbose --force) but it doesn't run each hour.

I tried several solutions by googling this problem like puting my program.conf in /etc/logrotate.d and moving logrotate from cron.dail into cron.hourly. but it doesn't work.

Here is my program.conf :

/home/user_i/*.log{
hourly
missingok
dateext 
rotate 1
compress
size 100M
sharedscripts
postrotate
    /usr/bin/bash file.sh
endscript
}

Have you any idea please ?

Thanks

Zadazadack answered 28/5, 2019 at 7:41 Comment(0)
C
7

On Debian Bullseye (and maybe other modern systemd based systems) logrotate is handled by a systemd timer which runs once a day. To change the run frequency of logroate to a hourly frequency, you have to override the default logrotate.timer unit.

Execute systemctl edit logrotate.timer and insert the following overrides:

[Timer]
OnCalendar=
OnCalendar=hourly
AccuracySec=1m

Then run systemctl reenable --now logrotate.timer to activate the changes.

The empty OnCalender option resets the previous defined values.

The default logroate.timer has set the AccuracySec option to one hour. Unfortunately resetting it with an empty value is not possible so it has to be set to one minute manually.

Consignment answered 24/11, 2021 at 19:19 Comment(0)
G
3

OP states in a comment that they can't use crontab and requires a solution utilizing /etc/cron.hourly.

  1. Take the "program.conf" file you're using to define the logrotate parameters and put that file somewhere accessible by the root user but NOT in the /etc/logrotate.d/ directory. The idea is that if we're running this hourly in our own fashion, we don't want logrotate to also perform this rotation when it normally runs. This file only needs to be readable by root, not executable.

  2. You need to make sure that ALL of the logrotate parameters you need are inside this file. We are going to be using logrotate to execute a rotation using only this configuration file, but that also means that any of the 'global' parameters you defined in /etc/logrotate.conf are not going to be taken into account. If you have any parameters in there that need to be respected by your custom rotation, they need to be duplicated into your "program.conf" file.

  3. Inside your /etc/cron.hourly folder, create a new file (executable by root) that will be the script executing our custom rotation every hour (adjust your shell/shebang accordingly):

#!/usr/bin/bash

logrotate -f /some/dir/program.conf

This will make cron fire off an hourly, forced rotation for that configuration file without having any effect on logrotate's normal functionality.

Gaffe answered 4/6, 2019 at 18:31 Comment(2)
Hello, many thanks, it worked, in addition to your solution, i changed some access rights of my log files (in /var/log/mylog_folder/)Zadazadack
@Zadazadack Glad to hear, please mark the answer as accepted if you have found it satisfactory to your question.Gaffe
N
1

You will need to add the job in your crontab

crontab -e

And then add a job that runs every hour 14 minutes past,

14 * * * * /usr/sbin/logrotate /home/sammy/logrotate.conf --state /home/sammy/logrotate-state

Taken from : https://www.digitalocean.com/community/tutorials/how-to-manage-logfiles-with-logrotate-on-ubuntu-16-04

Also additionally check if the crontab is actually running by doing

service crontab status

if it is stopped you can start it by doing

service crontab start
Numbskull answered 28/5, 2019 at 8:3 Comment(3)
Thanks for your answer, i already tested it, and it worked, but i can't use the crontab because i have another one.Zadazadack
do you know how to do it with /etc/cron.hourly/ please ?Zadazadack
@Zadazadack mv /etc/cron.daily/logrotate /etc/cron.daily. Also make sure your script has hourly logrotate setup.Dollarfish
A
-2

If you are using Linux, once you install logrotate rpm, it automatically creates a file logrotate under /etc/cron.daily. You can move this file to the /etc/cron.hourly folder. This will then automatically run logrotate hourly.

Aruba answered 19/7, 2022 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.