Cron task ScanFiles takes hours to finish

I am running on 19.0.4 right now, but the problem already started earlier. I assume that it started to appear when upgrading to NC19, but this could also be a false assumption.

Anyhow, after reading some threads in the forum I can conclude that the cron task
OCA\Files\BackgroundJob\ScanFiles takes forever to finish.

The oc_jobs database shows this information

4 OCA\Files\BackgroundJob\ScanFiles null 1603193402 1603193402 1603193402 46367

All other entries inside the table are below a minute or around 1 to 4 minute(s), but with the ScanFiles entry we talking about hours / half a day.

I am even not sure why ScanFiles is even needed. I removed the “External Storage” Plugin and all other data is pushed via WebDav or NC Clients / Web to the server, there are no external changes on the files being done.

Another problem is, that I do have multiple instances running at the same time, which, after some time / days, makes my server very sluggish and eats up CPU und RAM, and also starts occupieing the mysqld processes. This is after 24 hours uptime, 2 jobs have finished already with some hours in processing time.

image

The cron itself, is started every 5 minutes based on the official documentation
https://docs.nextcloud.com/server/19/admin_manual/configuration_server/background_jobs_configuration.html#cron and the information in nextcloud itself (https://nc.instance/settings/admin) saying cron should run every 5 minutes.

My questions are, why does cron even start multiple times if one is not finished?
And why does “ScanFiles” even run without external storage scans being activated, I could not find any information on this.

And perhaps also interesting, this instance runs on Debian 10, with 8GB RAM and Intel® Core™ i5-3320M CPU @ 2.60GHz

But yes, I have around 2TB of data being stored on the NC instance by around 10 users.

Is clearing the oc_jobs a solution for the problem or does it create more problems - like mentioned as a solution in this thread

Hi, yes, that would likely solve your issue. However, don’t do this: removing this standard Cron job is not a real solution. I deleted the contents of the jobs table because the Maps app had created endless new jobs. In your case, the number of jobs is not the issue, but one particular job does not run.

Sorry, cannot help you there. You might try: sudo -u www-data php [location of your nextcloud base dir]/occ files:scan --all to see if that fixes it.

I figured out, based on the fact that OCA\Files\BackgroundJob\ScanFiles takes forever, and the comment from @Castiontone, that I am not alone and there seems to be a bug related to symlinks created.

In my case the two accounts which break my cronjob and the ScanFilesore even the manual files:scan, both do have a “system backup” from a Window System there. Which most properly includes lots of Links / Symlinks which breaks everything.

There is a solution / push request for this already


but I have no clue when it gets applied to NC server.

Also other people report the same issue under different “name”


The problem for me was file locking via a table in the database. Switch to eg. redis or another memory cache. For redis install redis-server and do not forget to also install php-redis. Change the config.php as shown in https://docs.nextcloud.com/server/15/admin_manual/configuration_files/files_locking_transactional.html

Restart apache

php occ files:scan --all

for around 6800 files went from > 15 minutes to 3 seconds. The cron-job went from around 2 minutes to 10 seconds.

Thanks for the input, but in my case caching is working and activated. My NC instance would be unusable otherwise with many thousand files (perhaps even millions).
Anyhow, what I ran into is the BUG documentated and discussed here

1 Like