how to run a logrotate hourly

后端 未结 2 1722
野趣味
野趣味 2021-01-24 03:52

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<

相关标签:
2条回答
  • 2021-01-24 04:09

    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.

    0 讨论(0)
  • 2021-01-24 04:13

    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
    
    0 讨论(0)
提交回复
热议问题