Nextcloud Crontab/Cronjob Settings

The best/clearest info I have seen for using a cronjob with the preview generator app:
https://help.nextcloud.com/t/clarity-on-the-crontab-settings-for-the-preview-generator-app/6144/8

https://docs.nextcloud.com/server/stable/admin_manual/configuration_server/background_jobs_configuration.html

$ sudo crontab -e -u www-data

then enter the code below at the bottom (runs every 15 minutes):

*/15  *  *  *  * php -f /var/www/nextcloud/cron.php

How to run a cron manually (the example below is for nextcloud):

$ sudo -u www-data php -f /var/www/html/nextcloud/cron.php

Setup Cron (or systemd timers) for the Nextcloud Preview Generator

https://nextcloud.com/blog/setup-cron-or-systemd-timers-for-the-nextcloud-preview-generator/

Cron

Add or create a new cronjob for the Nextcloud user:

$ sudo crontab -e -u www-data

Using your favorite editor, add something like this, which will run the job at 04:00 (make sure to point to the correct directory of the ‘occ' file):

0 4 * * * /usr/bin/php -f /var/www/html/nextcloud/occ preview:pre-generate

Or using systemd timers

Systemd also has the possibility to run specific tasks at specific times or events. called Timers. You need to create 2 unit files:

File 1:

$ sudo nano /etc/systemd/system/nextcloud-preview-generator.service

and add this to the File 1:

[Unit]
Description=Nextcloud Preview Generator

[Service]
Type=oneshot
User=www-data
ExecStart=/usr/bin/php -f /var/www/html/nextcloud/occ preview:pre-generate

[Install]
WantedBy=basic.target

File 2:

$ sudo nano /etc/systemd/system/nextcloud-preview-generator.timer

and add this to File 2:

[Unit]
Description=Run Nextcloud Preview Generator daily at 04:00

[Timer]
OnCalendar=*-*-* 4:00:00
Persistent=true

[Install]
WantedBy=timers.target

systemd reload:

$ systemctl daemon-reload

activate the timer:

$ systemctl enable nextcloud-preview-generator.timer

start the timer:

$ systemctl start nextcloud-preview-generator.timer

Hosting

Develop and scale your apps globally with DigitalOcean and/or Vultr – or use shared hosting with no server maintenance required at iMarketHost.com.

Installation & Maintenance

If you would like to get this app installed, maintained or need training, Contact Me to get current rates.

6 thoughts on “Nextcloud Crontab/Cronjob Settings

  1. Thanks for the article, and something you could add for the people that run nextcloud in docker:

    Command to call from Cron when running Nextcloud in Docker:
    docker exec –user www-data CONTAINER_ID php /var/www/html/cron.php

    and, for the Previews:
    docker exec –user www-data CONTAINER_ID php occ preview:pre-generate

  2. Hi, Thank you for the articale. I have followed your instruction, somehow the cron job still not working as it should. A few moments later, the status went back to “Background jobs Last job execution ran… Something seems wrong” Under http://localhost/cron.php..it shows “{“data”:{“message”:”Backgroundjobs are using system cron!”},”status”:”error”}” even after I run the command “sudo -u www-data /usr/bin/php -f /var/www/nextcloud/cron.php”. Any help appreciated!

    1. Not sure what this might be but first you would want to 1) check the logs at ‘Settings > Logging’, and 2) double check your cron time settings while verifying that the path to Nextcloud is correct (which it seems it is if you got the “Last job ran” message).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.