Using crontab for scan files with OCC command

Nextcloud version : 22.2.6
Operating system and version : debian 11
Apache or nginx version : Apache 2
PHP version : 8.1

The issue you are facing:

Hello everyone,
i’m trying to automate a scan with OCC command using crontab (daily scan). But when the time for the crontab comes, it simply does nothing. No scan at all. I can confirm that because i’ve added /modify files before and after the crontab schedule, i can’t see any of my modification in NC.

Here is the file where i put my command :

sudo crontab -e -u www-data

Here is the command i use in the crontab :

www-data /usr/bin/php /var/www/nextcloud/occ files:scan --all --verbose 2>&1 >> /tmp/nextcloud_rescan_debug.log

the log file is totally empty.

And the command (without the verbose part) works perfectly well outside of the crontab.

thanks in advance for your help.

Try putting it in a file and running that file with cron instead. The output redirection may not work in the cron itself.


Why start with www-data
Shell will not see this as a command end exit.
Its already in the user www-data crontab.

1 Like

In addition to what @Vincent_Stans stated you also have to specify a time when the command should be executed. You can use to get the correct syntax.

If you for example want to run it daily at midnight you could use the following line:

@daily /usr/bin/php /var/www/nextcloud/occ files:scan --all --verbose 2>&1 >> /tmp/nextcloud_rescan_debug.log

If you want to run it every night at 2am it would look like this…

0 2 * * * /usr/bin/php /var/www/nextcloud/occ files:scan --all --verbose 2>&1 >> /tmp/nextcloud_rescan_debug.log

If it still doesn’t work, try it in combination with what @KarlF12 suggested.

ok but what kind of file do i have to create ? and how to run it instead of the OCC command ?

ok i will try !

i have it in my syntax, i just did’nt put it in my original post because this is not part of the problem (i guess). Thanks anyway.

Just a text file… a bash or sh script for example. Put your commands in it. Make it executable and run it from cron.

For example create a file in the root of your nc data-directory named or name it however you like and put it wherever www-data can read it.
Do place your command in this file.

echo “/usr/bin/php /var/www/nextcloud/occ files:scan --all --verbose 2>&1 >> /tmp/nextcloud_rescan_debug.log” | tee -a

make this file executable.

chmod +x
cp /nc-data-dir/

Now add this file to you crontab.

sudo crontab -e -u www-data
*/5 * * * * /nc-data-dir/

To avoid all above and add you command directly in cron you could try.

sudo crontab -e -u www-data
*/5 * * * * php -f /var/www/nextcloud/occ files:scan --all

Leave the logging as the system already log cron.

Thanks to both of you. I will try that and let you know :slight_smile: