Matomo 4 recommends using cron for archiving purposes. First you need to disable in system - general "browser archiving".

Then we can create a new cron job:

sudo vim /etc/cron.d/matomo-archive

Insert and modify where needed:

MAILTO="This email address is being protected from spambots. You need JavaScript enabled to view it."
5 * * * * www-data /usr/bin/php /var/www/html/matomo/console core:archive --url= > /var/log/matomo-archive.log

You can test and verify the outcome:

sudo su www-data -s /bin/bash -c "/usr/bin/php /var/www/html/matomo/console core:archive --url="


File permissions

Sometimes in system check you get errors like:

Try #: LOAD DATA INFILE : SQLSTATE[28000]: Invalid authorization specification: 1045 Access denied for user ‘matomo’@’%’ (using password: YES)[28000]
Try #: LOAD DATA INFILE : SQLSTATE[HY000]: General error: 13 Can't get stat of '/var/www/html/matomo/tmp/assets/matomo_option-d600bef9d230d99e92a7ce3b9541b49c.csv' (OS errno 13 - Permission denied),
Try #: LOAD DATA LOCAL INFILE : SQLSTATE[42000]: Syntax error or access violation: 3948 Loading local data is disabled; this must be enabled on both the client and server sides[42000]

To fix this you need to take the following steps:

First you need to add the global FILE permission to your matomo user

$ mysql> GRANT FILE ON *.* TO 'matomo'@'localhost';

Then you need to edit:

sudo vim /etc/mysql/conf.d/mysql.cnf

# add
local-infile = 1
local-infile = 1

# restart service
sudo service mysql restart

That should be it.