Nextcloud Docker Container - best way to run cron job

Nextcloud Docker Container - best way to run cron job

I have been doing some test setups of running NextCloud in a docker container. One thing I am still a little confused on and would like to hear your thoughts about, is running the cron process. I am a little surprised that it is not the default, but I found two methods of running the cron job in a container.

The first is from Docker Hub NextCloud examples (https://github.com/nextcloud/docker/tree/master/.examples). It “uses supervisor to run the cron job inside the container (so no extra container is needed). This image runs supervisord to start nextcloud and cron as two seperate processes inside the container.” This requires a custom image build (https://github.com/nextcloud/docker/blob/master/.examples/dockerfiles/cron/apache/Dockerfile). Other than that it is not complicated.

The second I found is also from the examples
(https://github.com/nextcloud/docker/tree/master/.examples/docker-compose/with-nginx-proxy/mariadb/apache). It is not explained well, but I believe they are just running a second NextCloud container to run the cron job. I am guessing this works because both containers use the same database and document store? Not sure about that.

My questions are:
What is the best way to run the cron job? (in regards to efficiency, ease of backups and restores, etc)
Is one an older way and the examples have just not been updated yet?

Thanks for your thoughts on this.

hello @John2 welcome back in the forum :handshake:

Please use the search - lot of issues have been discussed already like

there are multiple ways to run cron with docker. I think most common way is to setup another separate “cron” container using same image volume mounts, config and env variables as “app” with different entrypoint :

  dev-nextcloud-cron:
    image: nextcloud:${NEXTCLOUD_VERSION}
    container_name: dev-nextcloud-cron
    restart: unless-stopped
    env_file:
      - ./nextcloud.env
      - ./db.env
    volumes:
      - ./app:/var/www/html
      - ./files:/var/www/html/data
      - ./config:/var/www/html/config
    entrypoint: /cron.sh
    depends_on:
      - dev-nextcloud-app

I personally use:

docker exec -u www-data *containername* php /var/www/html/cron.php which runs via system cron

6 Likes

I found this advice to be the most helpful of them all, as anything else means meddling with dockerfiles and the like.

What is perhaps not mentioned very clearly is that this command needs to be added to the crontab of root on the host system.

sudo crontab -e

Add this line to your file:

*/5 * * * * docker exec -u www-data nextcloud php /var/www/html/cron.php

Close and save.

11 Likes

I use this now

  cron:
    image: nextcloud
    container_name: nextcloud-cron
    restart: unless-stopped
    depends_on:
      - app
#    env_file:
#      - ./nextcloud.env
#      - ./db.env
    volumes:
      - type: volume
        source: nextcloud_app.persistent
        target: /var/www/html
        read_only: false
      - type: volume
        source: nextcloud_data.persistent
        target: /var/www/html/data
        read_only: false
    entrypoint: /cron.sh

(Had to comment out the .env files, since I don’t have them). It starts fine, the logs of nextcloud-cron says:

nextcloud-cron
crond: crond (busybox 1.35.0) started, log level 0
crond: user:www-data entry:(null)
100001000010000100001000010000100001000010000100001000010000
111111111111111111111111
11111111111111111111111111111111
111111111111
1111111
crond: crond (busybox 1.35.0) started, log level 0
crond: user:www-data entry:(null)
100001000010000100001000010000100001000010000100001000010000
111111111111111111111111
11111111111111111111111111111111
111111111111
1111111
crond: user:www-data entry:(null)
100001000010000100001000010000100001000010000100001000010000
111111111111111111111111
11111111111111111111111111111111
111111111111
1111111
crond: wakeup dt=18
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=60
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=60
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=60
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond:  job: 0 php -f /var/www/html/cron.php
crond: child running /bin/bash
crond: USER www-data pid  13 cmd php -f /var/www/html/cron.php
crond: wakeup dt=10
crond: crond (busybox 1.35.0) started, log level 0
crond: user:www-data entry:(null)
100001000010000100001000010000100001000010000100001000010000
111111111111111111111111
11111111111111111111111111111111
111111111111
1111111
crond: user:www-data entry:(null)
100001000010000100001000010000100001000010000100001000010000
111111111111111111111111
11111111111111111111111111111111
111111111111
1111111
crond: wakeup dt=50
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=60
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=60
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=60
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond:  job: 0 php -f /var/www/html/cron.php
crond: child running /bin/bash
crond: USER www-data pid  12 cmd php -f /var/www/html/cron.php
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: file www-data:
crond:  line php -f /var/www/html/cron.php
crond:  job: 12 php -f /var/www/html/cron.php
crond: user www-data: process already running: php -f /var/www/html/cron.php
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: wakeup dt=10
crond: crond (busybox 1.35.0) started, log level 0
crond: user:www-data entry:(null)
100001000010000100001000010000100001000010000100001000010000
111111111111111111111111
11111111111111111111111111111111
111111111111
1111111
crond: user:www-data entry:(null)
100001000010000100001000010000100001000010000100001000010000
111111111111111111111111
11111111111111111111111111111111
111111111111
1111111
crond: wakeup dt=4
crond: file www-data:
crond:  line php -f /var/www/html/cron.php

But when checking the issues as admin on the nextcloud instance it says there’s an error with cron and it hasn’t been run for many days. I have selected cron as the service to run background jobs with. How can I trouble shoot?

Sanctimon’s post is the way if you have the capability to access host cron. only different thing that i do is i have the cron line execute a shell script which basically just has the same line listed in the post

1 Like

there is a reason for .env files. speaking in simple words cron it another instance of Nextcloud application running specific tasks - for this reason it must have access to config and database - provide same config as you do for application container itself.

Thanks for the advice. I had luck using this command, but had to use the container ID, not name.
Edit - my error, I was using the wrong container name. When I used the name provided in the docker-compose file, instead of the image name - it worked.

4 posts were split to a new topic: Run occ:files scan as cronjob in container

A post was merged into an existing topic: Run occ:files scan as cronjob in container