I rotate Nginx logs daily (with dateext). After the rotation I want to parse the file for the day that just passed and compile an email with the number of errors returned by Nginx server.

How can I access the output file in the postrotate / endscript section of logrotate ?

  • Can you elaborate more? – Khaled Nov 1 '10 at 11:09
  • Done. Sorry, I thought it's clear enough. :) – ibz Nov 2 '10 at 2:37

I'm not aware of any variables you can use if that's what you're looking for. However immediately after rotating the log, you should know precisely the name that the file has been rotated to based on the configuration you've set for the rotation (/var/log/somefile.1 or the like).

Perhaps it would be easier to answer if you described actual problem you're trying to solve?

  • I thought the problem is irrelevant. Explained it in more detail now. Yes, I can just use the date to get the file name, but I was hoping I can get it through some variable or something, which would make more sense. – ibz Nov 2 '10 at 2:39
  • 3
    Looks like larsks has hit on exactly what you want. $1 is the variable you're looking for. – mark Nov 2 '10 at 11:10

If you are not using the "sharedscripts" directive, then your postrotate script receives, as $1, the file that triggered the log rotation. This might help if you're trying to use a generic script with multiple logrotate stanzas. That is, given something like this:

/var/log/sample1.log /var/log/sample[23].log {

If any matching files needs to be rotated, your script will be called with $1 set to "/var/log/sample1.log", "/var/log/sample2.log", or "/var/log/sample3.log" as appropriate. You can then append ".1" to find the file that was just rotated.

If you use the "sharedscripts" option, then you script would be called with $1 set to "/var/log/sample1.log /var/log/sample[23].log" (which will help you identify a particular stanza but not the exact file).

Hopefully this gives you a place to start. Note that this will only work for logrotate > v3.7.5.

  • 1
    Handy. Actually, both the pre and post-rotate script appear to receive the rotated file name as $1, which is exactly what the question asks for. – mark Nov 2 '10 at 11:06
  • 1
    @mark the script receives the name of the input file, not the output file – kbolino Jul 28 '16 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.