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:
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:
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.
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.
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).
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.
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.
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:
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…