大网数据广告活动
服务器运维问答

How to move compressed files with logrotate

· 52CE 我爱测

This is probably insanely easy to do, but I can't find anything relevant to the topic. I'm setting up a custom logrotate.conf file for the logs in our service. As part of this, I would like to keep logs up to 7 days, compress ant…

问题原文

原帖链接 · SVill · CC BY-SA 4.0

This is probably insanely easy to do, but I can't find anything relevant to the topic. I'm setting up a custom logrotate.conf file for the logs in our service. As part of this, I would like to keep logs up to 7 days, compress antything older than 7 days, and delete anything older than 22 days. So far, I'm trying to test whether I can move compressed files to another directory, to keep the main log directory from becoming too cluttered.

My rules so far are as follows:

/home/user1/logs/profile_service/*.log
{
    daily
    rotate 7
    copytruncate
    compress
    postrotate
                mv /home/user1/logs/profile_service/*.gz /home/user1/logs/archive/profile_service/
    endscript
}

My understanding of this is that the postrotate should move those .gz files into the archive directory. However, when I manually run logrotate:

logrotate -f /home/user1/logrotate.conf

It rotates the logs and appends the date to the files, but fails to compress the logs, and nothing gets into archive:

mv: cannot stat ‘/home/user1/logs/profile_service/*.gz’: No such file or directory
logrotate_script: line 2: compress: command not found
error: error running non-shared postrotate script for /home/user1/logs/profile_service/profile.log of '/home/user1/logs/profile_service/*.log
'

When I do not include the postrotate to move the files, the files are compressed normally.

已采纳解答

原帖链接 · Bert · CC BY-SA 4.0

Try lastaction/endscript

From the man page:

The lines between lastaction and endscript (both of which must appear on lines by themselves) are executed (using /bin/sh) once after all log files that match the wildcarded pattern are rotated, after postrotate script is run and only if at least one log is rotated. These directives may only appear inside a log file definition. Whole pattern is passed to the script as first argument. If the script exits with error, just an error message is shown (as this is the last action). See also firstaction.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-11-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。