ubuntu日志分割配置


ubuntu 日志自动分割压缩 防止磁盘空间占满

/etc/logrotate.d/weixin_api

/var/www/weixin_api/shared/log/*.log {
    rotate 7
    daily
    compress
    delaycompress
    missingok
    notifempty
    copytruncate
    su root root
}

配置说明

  • daily – Rotate the log files each day. You can also use weekly or monthly here instead.
  • missingok – If the log file doesn’t exist, ignore it
  • rotate 7 – Only keep 7 days of logs around
  • compress – GZip the log file on rotation
  • delaycompress – Rotate the file one day, then compress it the next day so we can be sure that it won’t interfere with the Rails server
  • notifempty – Don’t rotate the file if the logs are empty
  • copytruncate – Copy the log file and then empties it. This makes sure that the log file Rails is writing to always exists so you won’t get problems because the file does not actually change. If you don’t use this, you would need to restart your Rails application each time.

测试配置文件 手动运行

sudo logrotate -f /etc/logrotate.d/weixin_api

不需要重启服务 规则会自动生效 通过 crontab