1

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.

1
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.