how to run a logrotate hourly

2020-05-06 16:49发布

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

2条回答
够拽才男人
2楼-- · 2020-05-06 17:31

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
查看更多
成全新的幸福
3楼-- · 2020-05-06 17:52

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 every day. 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.

查看更多
登录 后发表回答