2

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.

SVill
  • 77
  • 3
  • 13
  • Take a look at this answer. it might send you down the right path. https://stackoverflow.com/questions/7271945/logrotate-compress-files-after-the-postrotate-script – uSlackr Nov 07 '19 at 21:27
  • Why not use `olddir` command specifically designed for this purpose? – user247243 Nov 02 '21 at 11:03

1 Answers1

2

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.

Bert
  • 2,733
  • 11
  • 12