Python – Is it possible to schedule a python script to run at the end of logrotate

logrotatepiwikpython

I am using Piwik Analytics on a site that doesn't run any javascript and I would prefer to have data created from the access logs instead of from the image tracker – results from the logs are actually better because they can include static file downloads.

This is a LEMP server with Ubuntu 12.04 and nginx 1.4.5.

The python script is located at /var/www/example1.com/public/piwik/misc/log-analytics/import-logs.py and would need to run with several options. The access log is located at /var/www/example2.com/logs/access.log.1.

The relevant portion of /etc/logrotate.d/nginx contains:

/var/www/*/logs/*.log {
   daily
   missingok
   rotate 36500
   compress
   delaycompress
   notifempty
   create 0640 www-data adm
   sharedscripts
   prerotate
       if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
           run-parts /etc/logrotate.d/httpd-prerotate; \
       fi; \
   endscript
   postrotate
                [ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
        endscript
}

I don't know how to integrate a python script such that it will run at the end of this (after the rotations have completed).

Edit:

An additional issue I didn't consider initially is that this script only needs to run for one domain – the rest of the domains on the server do not require the script. The file above I created so that it would automatically rotate logs for all domains added to the server without my having to edit /etc/logrotate.d/nginx.

Best Answer

You can put the command you want to be executed between the postrotate and endscript lines (make sure to keep the line already there):

   postrotate
      [ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
      /usr/bin/python /var/www/example1.com/public/piwik/misc/log-analytics/import-logs.py somearguments
   endscript