“Nextcloud is sluggish, notifications arrive hours late and the trash never empties.” When I read a description like that, I don't open the logs first. I check one single setting: how do the background jobs run?

What background jobs do in Nextcloud

Nextcloud moves many tasks out of the actual request into background jobs: expiring old files in the trash and old versions, activity and notification emails, database cleanup, synchronisation with external services and plenty of things that apps register on top. If these jobs don't run regularly, they back up – and you notice it in many places at once.

The three modes: AJAX, Webcron, Cron

Under Administration settings → Basic settings → Background jobs there are three options:

  • AJAX: one job runs with every page load in the web interface. If nobody uses the browser – for example because everyone only syncs via the desktop client or the app – practically nothing happens.
  • Webcron: an external service calls cron.php over HTTP at regular intervals. Better than AJAX, but dependent on that service and on web server time limits.
  • Cron: the system itself starts cron.php at fixed intervals via the PHP command line. This is the recommended option.

AJAX is the default. For a production instance it is almost never enough.

Switching to cron

You can change the mode in the web interface or with occ:

sudo -u www-data php /var/www/nextcloud/occ background:cron 

Then create the cron job for the web server user – www-data on Debian and Ubuntu, usually apache on RHEL-based systems:

sudo crontab -u www-data -e 
*/5 * * * * php -f /var/www/nextcloud/cron.php 

Adjust the path to your installation. Five minutes is the recommended interval.

The user matters: cron.php refuses to run if the calling user doesn't own config/config.php. A job in root's crontab therefore won't work – or, worse, someone once “fixed” it by bending file permissions.

Alternative: a systemd timer

If you prefer systemd, create a service and a timer. /etc/systemd/system/nextcloudcron.service:

[Unit] Description=Nextcloud cron.php job [Service] User=www-data ExecStart=/usr/bin/php -f /var/www/nextcloud/cron.php KillMode=process 

/etc/systemd/system/nextcloudcron.timer:

[Unit] Description=Run Nextcloud cron.php every 5 minutes [Timer] OnBootSec=5min OnUnitActiveSec=5min Unit=nextcloudcron.service [Install] WantedBy=timers.target 
sudo systemctl daemon-reload sudo systemctl enable --now nextcloudcron.timer systemctl list-timers nextcloudcron.timer 

The upside: error output ends up in the journal (journalctl -u nextcloudcron.service) instead of in a mail to root that nobody reads.

Common pitfalls

Wrong PHP version: on servers with several PHP versions, the command-line php isn't necessarily the same version the web server uses. When in doubt, put the full path to the right binary in the job.

APCu as local cache: if memcache.local is set to APCu, APCu must also be enabled for the CLI (apc.enable_cli=1), otherwise the job aborts. Alternatively pass it in the call: php --define apc.enable_cli=1 -f /var/www/nextcloud/cron.php.

Large backlog: after a long time in AJAX mode, the first runs can take noticeably longer. That settles down.

How to check that it runs

  • In Basic settings, the time of the last run is shown next to “Cron”. If it's older than a few minutes, something is wrong.
  • Run it manually as the web server user and watch for errors: sudo -u www-data php -f /var/www/nextcloud/cron.php
  • Query the timestamp of the last run: sudo -u www-data php /var/www/nextcloud/occ config:app:get core lastcron (Unix time, readable with date -d @<value>).
  • Check the crontab or timer: crontab -u www-data -l or systemctl list-timers.
  • Look at the overview in Administration settings for warnings about background jobs.

The switch takes five minutes and fixes a whole set of symptoms that at first glance have nothing to do with each other.

Originally published at daniele-michelin.com. More guides, free checks (SPF, TLS, headers) and an error-message lexicon: daniele-michelin.com/en/.