Run occ:files scan as cronjob in container

So okay, this looks good and also match with the docs but this one here isn’t answered anywhere: Where can i set up the crontab itself? So you command is calling cron.php every 5 minutes and cron.php is doing his thing. Lets say i want to sync my files now every 5 minutes, before i added this in my crontab -e “sudo docker exec --user www-data nextcloud_app php occ files:scan --all” Where do i add know this “php occ files:scan --all” command after your command?

Your issue is perhaps that you are not pointing to where exactly occ is in your container.

You need to open up a terminal on your host machine. Then

sudo crontab -e

And at the end of that text file add your command:

*/5 * * * * docker exec -u www-data nextcloud php /var/www/html/occ files:scan -all

Ensuring that you are pointing to the correct location of occ in your container.

And this will need no cron docker container?

Or do i need a extra cron container an run the command like
*/1 * * * * docker exec -u www-data nextcloud_cron_container php /var/www/html/occ files:scan --all

Right now i stopped the cron container and just running my nc and db containers. I added this command */1 * * * * docker exec -u www-data nextcloud php /var/www/html/occ files:scan --all in my root crontab of my host system. I take a look in the web interface of nextcloud and see " Last job execution ran 12 minutes ago. Something seems wrong." So i guess it’s not running?

I get rid of this “something seems wrong” error if i deploy the cron container. So i guess i need the cron container AND ALSO a entry in my root crontab on my host?

I mean, it’s quite confusing as there are a lot of opinions and a lot of unanswered questions. Like, even if i create a cron container i need to add docker exec -u www-data nextcloud php -f /var/www/html/cron.php to my root cron on my host system in order to run it. But where can i add now my own crontabs? Like, i want to echo "helloworld" every minute INSIDE the nextcloud container. Where do i have to put the */1 * * * * echo "hellworld"? Inside the container? On my host? Inside the container into the cron.sh file? Somewhere else?

Please don’t not post unrelated questions into old topic but rather open new one and reference topics you find valuable. I’m going to split into separate conversation.

this command is not supposed to run on regular intervals. If you need to do so likely you are doing something strange with your system and should rethink you system usage.

My personal data are in some folders, just lets say its one folder. This one folder is a smb share because of reason. If i put a file into this folder from a system, no matter if its windows, macos or linux, it’s not visible in nextcloud web interface. It’s only visible after running php occ files:scan --all. So if i don’t use this command, i can’t see new files in nextcloud interface after moving/creating those in my smb share.

SMB share should be integrated using external_storage which allow regular files scan using internal mechanism. As long you have to run the command you better run it more precise only scanning the specific user/folder

1 Like

OK thanks, i will use the external storage app in future as i want to move all my data in the next months to another NAS.

But back to my main question: I created a cron container:

  cron:
    image: nextcloud:${NEXTCLOUD_VERSION}
    restart: always
    env_file:
      - db.env
      - smtp.env
    volumes:
      - nextcloud:/var/www/html
    entrypoint: /cron.sh
    depends_on:
      - app

After that, i create this entry in my root crontab:
*/5 * * * * docker exec -u www-data nextcloud_app_1 php -f /var/www/html/cron.php
which seems to trigger the cron.php file.

Now lets say i want to run my own cron. Whatever this is. This is why i choose this senseless */1 * * * * echo "hellworld" example - it can be everything. Where exactly do i setup */1 * * * * echo "hellworld? I looked around this forum here and googled but can’t find an answer. Everyone is saying “don’t use a container for this, just do docker exec [...] from host into the container to run a crontab whatever this is”, but this don’t seems to be the official solution. Thats why i am asking.

I don’t believe there is an “official” solution, as the Nextcloud Docker image is not officially supported and is a community maintained build.

If you want to run another cron inside the container (or any other container for that matter) for whatever other reason, you simply add your cron commands into your host crontab. Make sure you point to the correct container, as well as the correct location in the container.

It is also possible to set up a cron container, like you have, but I have found it to be superfluous, as my host crontab works just fine for my needs and maintaining another container just to run cron was a bit of an overkill. For others it might not be the case as they might be looking to do more complex things than just run a command at regular intervals.

Yeah and how do i create my own crontab for this cron container? Is it the same like creating a cronjob on my host system and point it to the cron container like

*/5 * * * * docker exec -u www-data nextcloud_cron php -f /var/www/html/cron.php
*/5 * * * * docker exec -u www-data nextcloud_cron php /var/www/html/occ whateveriwanttodo

# while the nextcloud instance itself is running in a container called nextcloud_app_1
1 Like

Write full path for php to avoid any confusion there.

The missing context is why do you need to run cron scan every 5 minutes? There is possibly a better solution.

1 Like

This is just an example. My main question is:

When i create cron container to run a crontab (no matter what it is, no matter how often i will run this), how can i still use exactly my own cronjobs.

Forget the cron containers. Useless complexity. System cron, or better yet, jus systemd periodic timers like this:

[~]$ cat .config/systemd/user/nextcloud-periodic-tasks.service
[Unit]
Description=Run nextcloud perdiodic jobs every 15-20 minutes

[Service]
Type=simple
ExecStart=/usr/bin/podman exec -u www-data nextcloud /usr/local/bin/php -f /var/www/html/cron.php


[~]$ cat .config/systemd/user/nextcloud-periodic-tasks.timer
[Unit]
Description=Run nextcloud perdiodic jobs every 15-16 minutes

[Timer]
OnBootSec=5min
OnUnitActiveSec=15min
RandomizedDelaySec=1min
AccuracySec=1

[Install]
WantedBy=default.target