2

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
}

3 Answers 3

6

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.

2

If anyone else stumbles across this it should be noted that logrotate supports a few scripts at various points as defined in the man pages.

man logrotate

Instead of using 'postrotate' option you can replace it with 'lastaction':

lastaction/endscript 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

1
/logs/*

^^^ Your wildcard is matching your *.gz files as well, and is rotating those in addition to the files you actually care about. Refine your matching rule to include only those file you're interested in and you should be all set.

1
  • Ah I didn't even think about that. Thanks for pointing that out. I'll write something to move them out to a different directory. The logs I take in are from any network source, and I write them as such. Like <hostname>_log. So a wildcard is my best chance at rotating them. So I can't write the logrotate script with specific log files because I'm not sure what they may be. Dec 18, 2015 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .