大网数据广告活动
服务器运维问答

logrotate by size - do I need to change the cron?

· 52CE 我爱测

My current logrotate configuration looks like this (using logrotate 3.8.7) "/var/log/haproxy.log" { daily size 250M rotate 1 create 644 root root missingok compress notifempty copytruncate } It successfully runs once a day, but w…

问题原文

原帖链接 · Hommer Smith · CC BY-SA 3.0

My current logrotate configuration looks like this (using logrotate 3.8.7)

"/var/log/haproxy.log" {
  daily
  size 250M
  rotate 1
  create 644 root root
  missingok
  compress
  notifempty
  copytruncate
}

It successfully runs once a day, but when size exceeds 250M it doesn't get rotated. So it waits until the end of the day to rotate, regardless of size.

I've read that there is the maxsize option, and after changing size 250M to maxsize 250M it still doesn't work.

Do I need to set a cron to run logrotate in an hourly manner, or logrotate gets run automatically because is listening to that file size, and I have something else wrong going on?

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 3.0

Yes, you need to run logrotate more than once a day to achieve this. This is answered in the seconds paragraph of the man page logrotate(8):

Normally, logrotate is run as a daily cron job. It will not modify a log multiple times in one day unless the criterion for that log is based on the log's size and logrotate is being run multiple times each day, or unless the -f or --force option is used.

If you would like to run logrotate hourly instead of daily, you can move it:

mv /etc/cron.daily/logrotate /etc/cron.hourly

Since 3.8.1 there has been maxsize. The difference between size and maxsize is described here:

maxsize size

Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval (daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive with the time interval options, and it causes log files to be rotated without regard for the last rotation time. When maxsize is used, both the size and timestamp of a log file are considered.

This would make both suitable for your use: size without any interval and maxsize if also interval is needed. However, this doesn't change the fact that by default logrotate runs only daily. That interval is suitable for most, as in most cases it takes a lot longer than a day for a log to grow over 250M.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。