【发布时间】:2022-01-07 13:15:52
【问题描述】:
我喜欢截断或调整由其他应用程序打开和使用的现有文件的大小。例如,随着时间的推移,磁盘中可能会出现大量占用大量空间的日志文件,例如 /var/log/secure。
在普通 Linux 中相当于
truncate -s 0 /var/log/secure
dd if=/dev/zero of=/var/log/secure bs=1M count=0
echo > /var/log/secure
根据
- How to empty ("truncate") a file on Linux that already exists and is protected in someway?
- How to create a file with a given size in Linux?
- How to create a file with ANY given size in Linux?
通过tail -F /var/log/secure,每个都会导致
==> /var/log/secure <==
tail: /var/log/secure: file truncated
tail: /var/log/secure: file truncated
tail: /var/log/secure: file truncated
而不是
==> /var/log/secure <==
tail: ‘/var/log/secure’ has been replaced; following end of new file
经过一些测试,似乎 Ansible v2.9 中的所有可用模块(file、lineinfile、copy 等)都将现有文件替换为新的空文件,但不会截断或调整大小并导致进入已经提到的
==> /var/log/secure <==
tail: ‘/var/log/secure’ has been replaced; following end of new file
我知道使用logrotate 旋转、压缩和删除日志文件可能是更好的解决方案。
【问题讨论】: