Centos – logrotate – run the postrotate after all processing

centoslogrotatesyslog

I have a logrotate script that is structured to rotate logs collected by syslog. Part of that script is to reload the syslog process. The problem is that the syslog reload runs for each matching log file it rotates and there are about 100 of them. How can I set up the logrotate script to reload the syslog process only once, after all individual logs have been processed?

/logs/* {
   daily
   rotate 7
   compress
   postrotate
      /etc/init.d/syslog-ng reload 2>/dev/null
   endscript
}

Best Answer

Use sharedscripts:

Normally logrotate runs the postrotate script every time it rotates a log. This is also true for multiple logs that use the same configuration block. For example, a web server configuration block that refers to both the access log and the error log will, if it rotates both, run the postrotate script twice (once for each file rotated). If both files are rotated, the web server is restarted twice.

To keep logrotate from running that script for every log, you can include the following command:

sharedscripts This command tells logrotate to check all the logs for that configuration block before running the postrotate script. If one or both of the logs is rotated, the postrotate script runs only once. If none of the logs is rotated, the postrotate script doesn’t run.