How to make preview generator cronjob?

Hello, I use the preview generator and can’t figure out how to do this step to automatically generate previews:

  1. Add a (system) cron job for ./occ preview:pre-generate

Below is my attempt:

sudo crontab -u www-data -e
23 * * * * “php /var/www/nextcloud/occ preview:pre-generate” 2>&1 | logger -t mycmd

mycmd log:

Apr 28 20:23:01 more-desktop mycmd: /bin/sh: 1: php /var/www/nextcloud/occ preview:pre-generate: not found

The below command to manually generate the preview images does work, which is why I don’t understand, how my crontab doesn’t run:

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

Nextcloud version (eg, 20.0.5): 26.0.0
Operating system and version (eg, Ubuntu 20.04): 18.04
Apache or nginx version (eg, Apache 2.4.25): 2.4.29
PHP version (eg, 7.4): 8.1

Check your crontab file (for example, sudo nano /etc/crontab). The command line should look like this (without php):

23 * * * * www-data /var/www/nextcloud/occ preview:pre-generate

Honestly, this seems odd, and I don’t know in which configuration on which platform this would be correct.

Why? How would it know that this is a php script that it has to parse and execute?

@NextMeIsOnline

On Debian and Ubuntu the following works for me: (I’m running it every 15 minutes)

sudo crontab -u www-data -e
*/15 * * * * php -f /var/www/nextcloud/occ preview:pre-generate

In your case, I would try this… (without the "):

23 * * * * php -f /var/www/nextcloud/occ preview:pre-generate 2>&1 | logger -t mycmd

If it still doesn’t work try it without the 2>&1 | logger -t mycmd part. I’m not sure what this is supposed to do and whether the syntax is correct or not.

If your goal is to write the output to separate log file you could try this:

23 * * * * php -f /var/www/nextcloud/occ preview:pre-generate > /path/to/logfile.log 2>&1
1 Like

@bb77

Honestly, this seems odd, and I don’t know in which configuration on which platform this would be correct.

On any version of ubuntu at least since 18.04

Why? How would it know that this is a php script that it has to parse and execute?

If I understand correctly, occ is a self contained binary file. Just try to run the same command in the terminal without “php” (for example, sudo -u www-data /var/www/nextcloud/occ preview:pre-generate). I use similar command to update music library in music app.

Ok. Not sure why this is working for you… Maybe you have created an alias or some variable globally on your system…?

On my system (Ubuntu 20.04) I get an error when I run it without php:

sudo: /var/www/html/nextcloud/occ: Befehl nicht gefunden (Command not found)

However I created a few bash aliases to make my life easier, when i have to manually run occ commands or for upgrading my Nextcloud.

alias ncupdate='sudo -u www-data php /var/www/html/nextcloud/updater/updater.phar'
alias nocc='sudo -u www-data php /var/www/html/nextcloud/occ'

So I just have to enter nocc <command> in order to run any occ commands or ncupdate in order to start the Nextcloud Updater.

Regardless of that, I would recommend to always use the full commands and paths in a crontab, unless you know exactly what you are doing and why you are doing it differently, because depending on how the maintainers of your distro configured things, aliases or variables may not be expanded to a non-interactive shell.

Anyways, if it works for you that’s of course fine, but I think it’s rather unsuitable as a general recommendation.

Perfect. That works.

I used the second half of the command to write a log file entry, which can be accessed with:

grep ‘mycmd’ /var/log/syslog

Whether that’s the best approach is beyond me, but it works.

In any case, previews are getting generated automatically now!

Edit:

without “php”, the command throws an error, so it’s definitely needed, unless an alias is set.

You have to chmod -x occ to enable execution without preceding php.