Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logrotate compress files after the postrotate script

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

like image 972
Thomas B in BDX Avatar asked Sep 01 '11 14:09

Thomas B in BDX


People also ask

What is Postrotate in logrotate?

Postrotate. Logrotate runs the postrotate script each time it rotates a log specified in a configuration block. You usually want to use this script to restart an application after the log rotation so that the app can switch to a new log.

How do you delay compression in logrotate?

The delaycompress option makes the compression to delay till the next rotation of the log file. Delaycompress parameter would be useful for the application servers which requires writing to the logs continuously and delay compression for a particular amount of time.

What is Sharedscripts in logrotate?

The sharedscripts means that the postrotate script will only be run once (after the old logs have been compressed), not once for each log which is rotated. Note that the double quotes around the first filename at the beginning of this section allows logrotate to rotate logs with spaces in the name.

What is Missingok in logrotate?

missingok : If the log file is missing, go on to the next one without issuing an error message. noolddir : Logs are rotated in the same directory the log normally resides in (this overrides the olddir option). daily : Log files are rotated every day.


2 Answers

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.

like image 183
Mark Avatar answered Sep 19 '22 06:09

Mark


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.

like image 42
JW-padded-to-three-chars Avatar answered Sep 22 '22 06:09

JW-padded-to-three-chars