gunicorn进程持有对logrotated文件的引用,导致磁盘空间用完

问题描述 投票:0回答:1

我正在使用logrotate来旋转gunicorn访问/错误日志。

{
  su root root
  missingok
  compress
  dateext
  dateformat .%Y%m%d
  notifempty
  sharedscripts
  postrotate
      # Send USR1 signal to the gunicorn master, which will cause it to reopen log files.
      # http://docs.gunicorn.org/en/latest/deploy.html#logging
      /bin/kill -USR1 $(cat /var/run/xxxx/api.pid 2>/dev/null) 2> /dev/null || true
  endscript
}

日志正确旋转并压缩,并创建新的日志文件。但是gunicorn不会释放指向已删除日志文件的指针,并继续写入该日志文件。因此,磁盘上的文件空间不会被释放,日志行也会丢失。

我可以看到lsof的条目

gunicorn  22284           root    9w   REG  252,1 43263609     0 117968 /usr/cachelogic/log/gunicorn_unapi_access.log-20190410 (deleted)

如果我重新启动gunicorn服务,文件空间将被释放,并且该过程还会将日志写入新文件。但是写入已删除文件的旧日志将丢失。

我想在不重新启动服务的情况下解决logrotate问题。如何确保将日志写入新日志文件而不是logrotated文件,并且正在释放磁盘空间。

python gunicorn logrotate
1个回答
0
投票

您正在寻找copytruncate选项。这就是文件系统的工作方式。当进程已将描述符打开到文件并且您将删除该文件(例如从命令行)时,描述符仍将在内存中可用(通过inode)。因此copytruncate将只复制您的日志文件,并且当前打开的日志文件(与gunicor连接)将截断为0。

© www.soinside.com 2019 - 2024. All rights reserved.