Setup Cron or systemd timers for the Nextcloud Preview Generator

Originally published at: https://nextcloud.com/blog/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
6 Likes

Nice, thanks for that! I was just running occ preview:pre-generate the other day manually.

You may consider adding those lines to the [Service] section, which will turn the service into a “background” service, meaning it will give up resources more gracefully:

[Service]
...
IOSchedulingClass=2
IOSchedulingPriority=7
...

Is there a specific reason for using Type=oneshot instead of Simple? The effect is that manually starting the service with systemd blocks until the preview generation has completed.
From the manual:

Behavior of oneshot is similar to simple; however, it is expected that the process has to exit before systemd starts follow-up units.

Also it would be awesome to run the preview generation more dynamically, e.g. when many new pictures have been uploaded.

1 Like

Thanks for this, also adding systemd timers to attention. Also Nextcloud cron.php could be done with this, removing the cron dependency.

Generally I am wondering about pros/cons between cron and systemd timers. systemd timers+units offer more possibilities to easily add requirements/dependencies before running the actual job, as well as logging behaviour and such.
When the cron daemon is not need, theoretically this frees some memory, on the other hand at least on Debian based distros many software titles and system tasks rely on cron anyway.

Congratulations for the big work, as usual!

I wanted to ask: could you add in the future the possibility to execute this previews generation via webcron as well? It would be very useful in case of shared hosting (usually with no direct access to server administration and no chance to execute commands like “php occ…” even via a php exec script).

Thank you

Is it possible that this command only exists for Nextcloud 14?

When I execute

sudo -u www-data /usr/bin/php -f /var/www/nextcloud/occ preview:pre-generate

on my Nextcloud 13.0.06 instance I get

[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the “preview” namespace.

Maybe you should mention this.

Have you tried to install the Preview Generator app before using a preview generator command? :wink:

I would like to ask how would I generate the preview pictures with external shares on nextcloud?

When I watch the diskusage of my preview files, it is even much bigger than the original images, because the preview files are generated I various sizes.

On the devoloper website at GitHub I found the information, that it is possible to generate only defined sizes instead of all sizes.

But which sizes are usefull or even mandatory?

I guess it depends on the apps you are using. In the activity apps and files app very small previews are shown. In the details page of files app, larger ones are shown and the largest in gallery. I guess when accessing with mobile agent this is again different, perhaps also depending on screen size? Would be indeed interesting to know a bid more details here, perhaps a GUI that allows to pick previews for certain apps/views inn long term.

I use the gallery and the normal file app, but usually not the details page.

I also use the mobile App for iPhone and iPad.

Great guide. FYI I am running Nextcloud 15 SNAP and as of 16 Feb 2019 I believe the path is /snap/bin/nextcloud.occ preview:pre-generate (I don’t feel very confident on this, however, as I am new to all this).

I want to run mine every 5 minutes. I entered the following:
sudo crontab -e -u MYUSERNAMEHERE

Then 2 lines under “# m h dom mon dow command” in the editor i entered:

*/5 * * * * /snap/bin/nextcloud.occ preview:pre-generate

But how do I know if my cronjob is running correctly? Is there a log somewhere I can view that shows when it last ran and if it completed successfully?

The guide shows an example : “0 4 * * * /usr/bin/php -f /var/www/nextcloud/occ preview:pre-generate”… but I am not sure if the portion “php -f” exists any longer? I ask because on this page https://docs.nextcloud.com/server/15/admin_manual/configuration_server/occ_command.html there is an instruction example “sudo -u www-data php occ” and when I run this I get “command not found” in my terminal. So is the PHP command not in Nextcloud 15?

Just not confident I got the cronjob all correct…

I followed the description in the FAQ section of the README.md file and have set the following parameters, which a sufficient for my needs:

> ./occ config:app:get previewgenerator squareSizes
32 64 1024
> ./occ config:app:get previewgenerator widthSizes
64 128 1024
> ./occ config:app:get previewgenerator heightSizes
64 256 1024

To give you an idea how the generated files could look like, here the content of one preview directory:

144-256.jpg         19567
256-256-crop.jpg    30883
36-64.jpg            2526
576-1024-max.jpg   200807
576-576-crop.jpg   119399
64-114.jpg           5393
64-64-crop.jpg       3576