How to setup a crontab to automatically archive overnight

In order to trigger automatically the Piwik archives at night, you can setup a crontab. A crontab is a time-based scheduling service in Unix-like server. You need SSH access to your server in order to set it up:

# crontab -e

and then add the lines:

5 0 * * * /usr/bin/php5 /path/to/piwik/misc/cron/archive.sh

The file archive.sh must be updated to contain your API key. You can find the API key in the User administration interface in Piwik

This will trigger the day / week / month / year archiving process at 00:05AM every day. This will make sure that when you visit your Piwik Interface, the data has already been processed: Piwik will load fast.

Make sure the crontab will actually work by executing the command /usr/bin/php5 /path/to/piwik/misc/cron/archive.sh in the shell. You should see XML output containing your number of visits for each date.

Increase the memory

If you have the error Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate X bytes), you can increase the memory allocated to PHP. Update your /etc/php5/cli/php.ini file, and set for example:

memory_limit = 128M

This will only affect PHP CLI, your webserver configuration stays unchanged. The crontab archiving should now work, and Piwik should be very fast!

Medium to High traffic

You may want to set two settings in your config/global.ini.php:

time_before_archive_considered_outdated = 3600
enable_browser_archiving_triggering = false

This will disable the automatic archiving from the browser, making sure your Piwik users don't trigger the heavy archiving process. Also, today's statistics will have a one hour lifetime, which makes sure the reports are not processed too often.