在没有 logrotate 的情况下保持日志文件大小固定

ura*_*ray 14 size logs logrotate files

有没有办法保持日志文件的文件大小固定,而无需通过新的空文件旋转它并删除(或归档)旧文件。例如,如果我将日志文件的最大大小设置为 1MB,则在文件大小增加超过该限制后,它将被自动夹紧,将文本添加到“尾部”并弹出文本的最旧部分以保持文件大小为 1MB .

jjc*_*son 5

您可以编写一个小 bash 脚本来执行此操作。只需使用tail -c并覆盖文件将文件拖尾到某个字节数即可。

来自man tail

-c, --bytes=N
              output the last N bytes; alternatively, use +N to  output  bytes
              starting with the Nth of each file

   If  the  first  character of N (the number of bytes or lines) is a `+',
   print beginning with the Nth item from the start of each  file,  other?
   wise, print the last N items in the file.  N may have a multiplier suf?
   fix:  b  512,  kB  1000,  K  1024,  MB  1000*1000,  M   1024*1024,   GB
   1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
Run Code Online (Sandbox Code Playgroud)

  • 我比较确定,一旦您“覆盖文件”,日志记录将不会在新文件上自动恢复。 (3认同)