我有一个服务,在启动时自动创build一个带有时间戳记的文件的日志文件。 因此,我不需要logrotate来重命名/复制/创build文件,但是我想要的是logrotate应该只保留这三个最新的文件(并可以压缩它们)。 我能做些什么吗?
我不确定你可以使用logrotate来做到这一点。 你可以运行以下作为每日cron作业:
rm $(ls -t | sed -e '1,3d')
我不认为这可以完全按照你的意愿来完成。 如果您还想先使用logrotate,则需要指定logrotate多长时间轮换您的日志(每日/每周/每月/每年)。 你可以像这样设置:
# rotate log files daily daily # Log files are rotated count times before being removed or mailed to the address # specified in a mail directive. If count is 0, old versions are removed rather than # rotated. rotate 3 # Old versions of log files are compressed with gzip(1) by default. compress
但正如@quanta写的,它不可能用logrotate完成。 您将使用上面写的类似的设置,或者您需要使用其他工具。