温馨提示×

Ubuntu中如何设置logrotate的日志保留周期

小樊
104
2024-08-08 20:18:40
栏目: 智能运维

在Ubuntu中,您可以通过编辑logrotate的配置文件来设置日志保留周期。以下是一些步骤:

  1. 打开logrotate的配置文件:sudo nano /etc/logrotate.conf

  2. 在配置文件中查找或添加一个日志文件的配置块,例如:

/var/log/nginx/*.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        /etc/init.d/nginx reload > /dev/null
    endscript
}

在上面的示例中,rotate 7表示保留7个旧日志文件,可以根据您的需求修改此值。

  1. 保存并关闭文件,然后检查logrotate的配置是否正确:sudo logrotate -d /etc/logrotate.conf

  2. 如果一切正常,您可以手动运行logrotate来立即对日志进行轮转:sudo logrotate -f /etc/logrotate.conf

  3. 您也可以将logrotate添加到cron作业中,以便定期进行日志轮转。编辑cron表:sudo crontab -e,然后添加以下行来每天执行logrotate任务:

@daily /usr/sbin/logrotate -f /etc/logrotate.conf

保存并关闭文件,cron将每天执行logrotate任务,按照配置文件中的设置进行日志轮转。

0