16

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 ?

reallynice
  • 83
  • 2
  • 9
ibz
  • 345
  • 4
  • 8

2 Answers2

19

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 {
  ..config...
}

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.

larsks
  • 43,623
  • 14
  • 121
  • 180
  • 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 02 '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
4

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?

mark
  • 2,365
  • 14
  • 11
  • 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 02 '10 at 02:39
  • 3
    Looks like larsks has hit on exactly what you want. $1 is the variable you're looking for. – mark Nov 02 '10 at 11:10