📜 ⬆️ ⬇️

Logrotate. Postrotate script and file name

I think that there is no * nix-admin who does not use the logrotate utility. Often we use it without even knowing that it is installed in the system. However, when I needed to parse the logged log file, I was extremely surprised.


So, the task is simple as a piglet's penny, and it is much more common. It is necessary to parse the log after rotation. Suppose we have a text log squid'a lying in / var / log / squid. We have /etc/logrotate.d/squid with the following content:
/var/log/squid/*.log { daily missingok rotate 8 compress copytruncate delaycompress notifempty nocreate sharedscripts postrotate /etc/init.d/squid reload > /dev/null endscript } 

That is, we rotate the log file once a day, after rotation we restart squid , we don’t click on the freshly rotated file. This suggests an obvious solution - add a call to our handler to postrotate , like this:
  postrotate /etc/init.d/squid reload > /dev/null /--/sq_control/manage.py squid --load-log $FILENAME endscript 

However, it was not there. There is nothing in manas about those passed in pre / postrotate. Nothing at all. As if they are not there and no one needs them. Googling also did not give any tangible results. However, by typing, it was found out that the variable $ 1 contains the file name. The truth is not what is obtained after the rotation, but what fits under the mask. In our case, /var/log/squid/access.log is not what is needed, but also bread. Knowing that logrotate standardly adds the suffix -YYYYMMDD and having sed at hand, we get:
  postrotate /etc/init.d/squid reload > /dev/null fffn=$1"-"`date '+%Y%m%d'` fffn=`echo "$fffn" | sed -r 's/\s+//g'` /--/sq_control/manage.py squid --load-log $fffn endscript 


If there is a better solution - share and criticize.

')

Source: https://habr.com/ru/post/145267/


All Articles