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 suspecting that it is installed in the system. However, when I needed to parse a targeted log file, I was extremely surprised.


    So, the task is simple as a nickel of a piglet, but it occurs much more often. It is necessary to parse the log after rotation. Let's say we have a squid'a text log located in / var / log / squid. We have /etc/logrotate.d/squid as follows:
    /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 do not click on the newly-released file. The obvious solution is to add a call to our handler to postrotate , something like this:
        postrotate
            /etc/init.d/squid reload > /dev/null
            /где-то-там/sq_control/manage.py squid --load-log $FILENAME
        endscript
    

    However, there it was. The mana says nothing about pre / postrotate transfers. Nothing at all. As if they are not there and nobody needs them. Googling also did not produce any tangible results. However, using the poke method, it was found out that the variable $ 1 contains the file name. The truth is not what happens after rotation, but what fits under the mask. In our case, /var/log/squid/access.log is not what you need, but bread too. 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.

    Also popular now: