Question

I have an application generating a really heavy big log file every days (~800MB a day), thus I need to compress them but since the compression takes time, I want that logrotate compress the file after reloading/sending HUP signal to the application.

/var/log/myapp.log {
    rotate 7
    size 500M
    compress
    weekly
    postrotate
        /bin/kill -HUP `cat /var/run/myapp.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

Is it already the case that the compression takes place after the postrotate (which would be counter-intuitive)? If not Can anyone tell me if it's possible to do that without an extra command script (an option or some trick)?

Thanks Thomas

Was it helpful?

Solution

The postrotate script does run before compression occurs: from the man page for logrotate

The next section of the config files defined how to handle the log file /var/log/messages. The log will go through five weekly rotations before being removed. After the log file has been rotated (but before the old version of the log has been compressed), the command /sbin/killall -HUP syslogd will be executed.

In any case, you can use the delaycompress option to defer compression to the next rotation.

OTHER TIPS

Adding this info here in case of anyone else that comes across this thread when actually searching for wanting a way to run a script on a file once compression has completed.

As suggested above using postrotate/endscript is no good for that.

Instead you can use lastaction/endscript, which does the job perfectly.

The postrotate script always runs before compression even when sharedscripts is in effect. Hasturkun's additional response to the first answer is therefore incorrect. When sharedscripts is in effect the only compression performed before the postrotate is for old uncompressed logs left lying around because of a delaycompress. For the current logs, compression is always performed after running the postrotate script.

@Hasturkun - One cannot add a comment unless their reputation is first above 50.

To make sure of what logrotate will do, either

  1. test your configuration with, -d: debug which tests but does not do anything, and -f: force it to run
  2. or you can execute logrotate with the -v verbose flag

With a configuration that uses a sharedscript for postrotate

$ logrotate -d -f <logrotate.conf file>

Shows the following steps:

rotating pattern: /tmp/log/messages /tmp/log/maillog /tmp/log/cron
...
renaming /tmp/log/messages to /tmp/log/messages.1
renaming /tmp/log/maillog to /tmp/log/maillog.1
renaming /tmp/log/cron to /tmp/log/cron.1
running postrotate script
<kill-hup-script executed here>
compressing log with: /bin/gzip
compressing log with: /bin/gzip
compressing log with: /bin/gzip
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top