Setup Cron or systemd timers for the Nextcloud Preview Generator

Guest post by @morph027

If you’re using the fantastic Nextcloud and like to feel comfortable browsing all your auto-uploaded photos like once upon a time you did in some proprietary Picasa or so, you can benefit from the Preview Generator, which will take care of pre-rendering the thumbnails. This means faster, smoother scrolling and previewing your images, both in your browser and on mobile devices!

As it states you must add some cron magic to get it working and the Nextcloud folks asked for a tutorial, here’s how to do it with cron or (my preference) with systemd timers.

Prerequisites

  • we need to setup the job as the system user running Nextcloud (e.g. www-data, nginx, httpd, … – depends on your distribution). You can find out by looking at the owner of the nextcloud files using ls -la
  • we need to use the absolut path to the php executable (which php)
  • we need to use the absolut path to Nextclouds occ file, depends in which folder your Nextcloud has been installed (e.g. /var/www/nextcloud/occ)

Cron

Add or create a new cronjob for the Nextcloud user:

crontab -e -u www-data

Using your favourite editor, add something like this, which will run the job at 04:00:

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

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:

/etc/systemd/system/nextcloud-preview-generator.service

[Unit]
Description=Nextcloud Preview Generator

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

[Install]
WantedBy=basic.target

/etc/systemd/system/nextcloud-preview-generator.timer

[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

Start the discussion at the
Nextcloud forums

Go to Forums
This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.