logrotate 里面应该使用copytruncate

1. 参考http://unix.stackexchange.com/questions/33447/why-we-should-use-create-and-copytruncate-together

copytruncate

Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

2. 原来产品里面使用的是 使用命令压缩备份文件(mv a.out a.out.1; gzip a.out),然后重启进程。如果进程重启不成功,会导致原来的log文件会仍被原有进程使用,并不断增长,最终导致磁盘空间不足。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
logrotate 是一个用于管理日志文件的工具,它允许你自动轮转、压缩、删除和管理日志文件。通过 logrotate,你可以定期对日志文件进行轮转,以便保持文件的大小和数量在可控范围内。 使用 logrotate 的基本步骤如下: 1. 创建一个配置文件:在 `/etc/logrotate.d/` 目录下创建一个新的配置文件,命名为你要管理的日志文件。 例如,如果要管理 `/var/log/nginx/access.log`,可以创建一个名为 `nginx` 的配置文件:`/etc/logrotate.d/nginx`。 2. 编辑配置文件:在配置文件中指定日志文件的详细信息和轮转规则。 以下是一个简单的示例配置文件: ``` /var/log/nginx/access.log { rotate 7 # 保留最近的7个日志文件 daily # 每天执行一次轮转 missingok # 如果日志文件不存在,也不报错 compress # 压缩轮转后的日志文件 delaycompress # 在下一次轮转时才压缩 notifempty # 如果日志文件为空,不进行轮转 create 0644 # 创建新的轮转后日志文件的权限 sharedscripts # 在所有脚本执行前执行 postrotate 脚本 postrotate /usr/bin/systemctl reload nginx # 轮转后重新加载 Nginx endscript } ``` 3. 测试配置文件:使用 `logrotate` 命令进行配置文件的测试,检查是否有语法错误: ``` logrotate -d /etc/logrotate.d/nginx ``` 4. 执行轮转:使用 `logrotate` 命令执行轮转操作: ``` logrotate /etc/logrotate.d/nginx ``` 5. 自动化轮转:通常,你会将 logrotate 添加到计划任务中,以便定期自动执行轮转。这可以通过编辑 `/etc/crontab` 文件来完成。 添加类似以下的行来定义 logrotate 的执行频率: ``` 0 0 * * * root /usr/sbin/logrotate /etc/logrotate.conf ``` 以上是一个简单的 logrotate 配置和使用示例。你可以根据实际需求来自定义配置文件中的参数和轮转规则。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值