# Docker setup & cron

**URL:** <https://help.nextcloud.com/t/docker-setup-cron/78547>\
**Category:** 📦 Appliances (Docker, Snappy, VM, NCP, AIO)\
**Tags:** docker, cron\
**Created:** [April 20, 2020, 10:45am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547 "2020-04-20T10:45:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dakky](https://help.nextcloud.com/letter_avatar/dakky/32/5_5575768a8748004e209b776fc1b2916d.png) [@dakky](https://help.nextcloud.com/u/dakky)\
**Post date:** [April 20, 2020, 10:45am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/1 "2020-04-20T10:45:09Z")

</div>

Hello

I’m running my nextclud instance via docker (compose file adopted from [here](https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/insecure/mariadb-cron-redis/apache/docker-compose.yml)). There is already a container running /cron.sh, which starts a busybox with cron, which starts cron.php every 5 minutes.

Now my question is: how to do i add additional tasks? (i.e. ./occ fulltextsearch:index)

I do not want to rebuild the nextcloud image just for adding a cron task. And i do not want to use the host for managing tasks in the container (host systemd oder host cron)

any way to manage this?

Greetings  
Dakky

---

<div class="post-metadata">

**Author:** ![kevdog](https://help.nextcloud.com/letter_avatar/kevdog/32/5_5575768a8748004e209b776fc1b2916d.png) [@kevdog](https://help.nextcloud.com/u/kevdog)\
**Post date:** [April 20, 2020, 12:22pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/2 "2020-04-20T12:22:46Z")

</div>

What’s your host system?

---

<div class="post-metadata">

**Author:** ![dakky](https://help.nextcloud.com/letter_avatar/dakky/32/5_5575768a8748004e209b776fc1b2916d.png) [@dakky](https://help.nextcloud.com/u/dakky)\
**Post date:** [April 20, 2020, 12:42pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/3 "2020-04-20T12:42:24Z")

</div>

the host is running ubutu LTS

---

<div class="post-metadata">

**Author:** ![KarlF12](https://help.nextcloud.com/user_avatar/help.nextcloud.com/karlf12/32/46873_2.png) [@KarlF12](https://help.nextcloud.com/u/KarlF12)\
**Post date:** [April 20, 2020, 12:49pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/4 "2020-04-20T12:49:52Z")

</div>

> [@dakky](#):
>
> I do not want to rebuild the nextcloud image just for adding a cron task.

Maybe you should. [They have a Dockerfile for doing this](https://github.com/docker-library/docs/blob/master/nextcloud/README.md#adding-features), and with docker-compose, the process is basically automatic.

---

<div class="post-metadata">

**Author:** ![Reiner\_Nippes](https://help.nextcloud.com/user_avatar/help.nextcloud.com/reiner_nippes/32/9479_2.png) [@Reiner\_Nippes](https://help.nextcloud.com/u/Reiner_Nippes)\
**Post date:** [April 20, 2020, 5:41pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/5 "2020-04-20T17:41:42Z")

</div>

> [@dakky](#):
>
> how to do i add additional tasks?

i just run them as cron jobs on the host. put them in small skript `/usr/local/bin/nextcloud_optimize.sh` and put this roots crontab.

> <https://github.com/ReinerNippes/nextcloud_on_docker/blob/bab3d3b24a83ec87fcfed9ad230e3eb186ee10df/roles/nextcloud_config/tasks/main.yml#L56>

> [@dakky](#):
>
> And i do not want to use the host for managing tasks in the container

any reason for this?

* * *

did you try to copy the [cron.sh](https://github.com/nextcloud/docker/blob/master/18.0/apache/cron.sh) to your host, add your tasks and run the container like this:

```auto
  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html
      - /path/to/your/cron.sh:/crons.sh:ro
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

```

---

<div class="post-metadata">

**Author:** ![R0Wi](https://help.nextcloud.com/user_avatar/help.nextcloud.com/r0wi/32/18933_2.png) [@R0Wi](https://help.nextcloud.com/u/R0Wi)\
**Post date:** [May 22, 2020, 3:30pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/6 "2020-05-22T15:30:05Z")

</div>

Hey guys,

as far as i can tell the crontab entry is done [here](https://github.com/nextcloud/docker/blob/ccdf46609ff8419ffd7c5ce4e51a117e378b72b6/Dockerfile-debian.template#L15). So as you can see, inside the container the file `/var/spool/cron/crontabs/www-data` is used and it usually contains one line like `*/5 * * * * php -f /var/www/html/cron.php` for executing the `cron.php` every 5 minutes. If you want to do any additional cron tasks you could mount a custom crontab file and add any desired lines. A snippet of the corresponding `docker-compose` file could look like this:

```auto
cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html
      - ./mycronfile:/var/spool/cron/crontabs/www-data
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

```

while the content of `mycronfile` could be something like

```auto
*/5 * * * * php -f /var/www/html/cron.php
0 0 * * 0 php -f /var/www/html/occ fulltextsearch:index

```

which will additionally index your NC instance once a week on sunday night at 0:00.

---

<div class="post-metadata">

**Author:** ![lazmol](https://help.nextcloud.com/letter_avatar/lazmol/32/5_5575768a8748004e209b776fc1b2916d.png) [@lazmol](https://help.nextcloud.com/u/lazmol)\
**Post date:** [August 28, 2020, 11:06am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/7 "2020-08-28T11:06:37Z")

</div>

Hello,

could you please show an example on how to add a task to this script?  
I could not find any examples on how to use this cron.sh. It seems to be a normal bash script but then how do you add a task e.g. for preview generator (`occ preview:pre-generate`)? I do not understand the “exec busybox crond” line already in that file, why do you need exec?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Reiner\_Nippes](https://help.nextcloud.com/user_avatar/help.nextcloud.com/reiner_nippes/32/9479_2.png) [@Reiner\_Nippes](https://help.nextcloud.com/u/Reiner_Nippes)\
**Post date:** [August 28, 2020, 3:15pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/8 "2020-08-28T15:15:57Z")

</div>

> [@Reiner\_Nippes](#):
>
> ```auto
> #!/bin/bash
> docker exec redis redis-cli -a <your-redis-password> FLUSHALL
> docker exec --user www-data nextcloud php occ files:scan --all
> docker exec --user www-data nextcloud php occ files:scan-app-data
> exit 0
> 
> ```

use this script as cronjob. do not try to enable cron inside the contaier. inside any container.

so you just add `docker exec --user www-data nextcloud php occ preview:pre-generate` assuming that your container name is nextcloud and www-data the web server user.

just saying.

> [@lazmol](#):
>
> the “exec busybox crond” line

> <https://github.com/nextcloud/docker/blob/488378f8e88071a68bec5c0f846c294fb61ddd76/18.0/apache/cron.sh#L4>

this is a way to enable crond inside a container.

`exec` executes everything you use as the first argument. e.g. a shell skript has no +x bit. `exec that-skript` is your friend. and `busybox crond` just starts the cron daemon inside the container.

nevertheless after i wrote the comment above i found this article in german [Projekt Rootserver - geschlossen](https://www.projekt-rootserver.de/cron-events-in-docker-containern-zum-laufen-bringen/2019/09/) it says: don’t do it with busybox. not a good idea. the authors advice: use the host cron + docker exec

---

<div class="post-metadata">

**Author:** ![lazmol](https://help.nextcloud.com/letter_avatar/lazmol/32/5_5575768a8748004e209b776fc1b2916d.png) [@lazmol](https://help.nextcloud.com/u/lazmol)\
**Post date:** [August 28, 2020, 8:30pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/9 "2020-08-28T20:30:36Z")

</div>

Thanks @Reiner_Nippes. I will then use system cron. I use [this docker-compose config to run my nextcloud](https://github.com/nextcloud/docker/tree/master/.examples/docker-compose/with-nginx-proxy/mariadb-cron-redis/fpm) and since there is a docker service for cron I thought I should use that but then that’s not recommended.

---

<div class="post-metadata">

**Author:** ![maste9](https://help.nextcloud.com/user_avatar/help.nextcloud.com/maste9/32/7511_2.png) [@maste9](https://help.nextcloud.com/u/maste9)\
**Post date:** [November 30, 2020, 10:27am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/10 "2020-11-30T10:27:11Z")

</div>

I just migrated my nextcloud to a docker setup on a NAS as host system. First I used the second container solution but now switched to your proposal of using a script in the host’s cron service.

Yet I have two questions that maybe you or s.o. else have answers to:

1. Do these commands

> ```
> docker exec --user www-data nextcloud php occ files:scan --all
> docker exec --user www-data nextcloud php occ files:scan-app-data
> docker exec --user www-data nextcloud php occ preview:pre-generate
> exit 0
> 
> ```

represent **all relevant jobs** which are usually done by cron.php?

1. Does this solution have any advantage against for example **calling cron.php as a webjob** like e.g. [https://zeitfresser.net/nextcloud-cronjob-ueber-synology-erstellen/](https://zeitfresser.net/nextcloud-cronjob-ueber-synology-erstellen/) (de) proposes?

Oh, and a bonus question if I may:

1. It is a tiny setup for max 5 users mainly using the cloud during daytime. So I’d like to let my NAS sleep during nighttime. A 24/7 every-15-minutes cronjob would prevent this, right? **Is there any risk if I pause the cronjob during night hours ?**

---

<div class="post-metadata">

**Author:** ![castrillo](https://help.nextcloud.com/user_avatar/help.nextcloud.com/castrillo/32/27827_2.png) [@castrillo](https://help.nextcloud.com/u/castrillo)\
**Post date:** [December 2, 2020, 9:41am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/11 "2020-12-02T09:41:58Z")

</div>

yesterday I was fixing my cron job, cos my deployment is running in docker-compose statement, so yes, as you said, I created a cronjob into the host to call a simple:

`docker exec -t -u www-data nextcloud-app php -f /var/www/html/cron.php`

in fact, from my beginner understanding from all about containers, It should be better run it on this mode. We should leaves each container running its own service. 😀

---

<div class="post-metadata">

**Author:** ![jlgarnier](https://help.nextcloud.com/user_avatar/help.nextcloud.com/jlgarnier/32/14742_2.png) [@jlgarnier](https://help.nextcloud.com/u/jlgarnier)\
**Post date:** [September 27, 2021, 6:44am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/12 "2021-09-27T06:44:38Z")

</div>

Hi all,

Sorry to dig the topic up, but I recently had conflicts with cron tasks: can anyone please confirm that the above cronjob `docker exec -t -u www-data nextcloud-app php -f /var/www/html/cron.php` is created for the www-data user (`crontab -u www-data -e`) and not for the root user?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![Reiner\_Nippes](https://help.nextcloud.com/user_avatar/help.nextcloud.com/reiner_nippes/32/9479_2.png) [@Reiner\_Nippes](https://help.nextcloud.com/u/Reiner_Nippes)\
**Post date:** [September 27, 2021, 3:52pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/13 "2021-09-27T15:52:28Z")

</div>

> [@jlgarnier](#):
>
> the above cronjob `docker`

should run as root on the docker host. or any user that is a member of the group “docker” on the host.

---

<div class="post-metadata">

**Author:** ![castrillo](https://help.nextcloud.com/user_avatar/help.nextcloud.com/castrillo/32/27827_2.png) [@castrillo](https://help.nextcloud.com/u/castrillo)\
**Post date:** [September 28, 2021, 5:41pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/14 "2021-09-28T17:41:44Z")

</div>

regarding the cron.php my user www-data is the owner, hence you should run it as Reiner said or by the owner/group member.

---

<div class="post-metadata">

**Author:** ![jlgarnier](https://help.nextcloud.com/user_avatar/help.nextcloud.com/jlgarnier/32/14742_2.png) [@jlgarnier](https://help.nextcloud.com/u/jlgarnier)\
**Post date:** [October 1, 2021, 12:01pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/15 "2021-10-01T12:01:27Z")

</div>

Hi all,  
I created/updated the crontab for the “root” user according to your advice:

```auto
30 1 * * * docker exec -t -u www-data php -f cron.php

```

and still get the (translated from French) “the last cron task ran 5 days ago. Something went wrong. […]”.

Is there any way to get only the cron log from the container?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![vomov](https://help.nextcloud.com/user_avatar/help.nextcloud.com/vomov/32/37928_2.png) [@vomov](https://help.nextcloud.com/u/vomov)\
**Post date:** [October 5, 2021, 8:25am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/16 "2021-10-05T08:25:12Z")

</div>

@jlgarnier: if you run cron from the host, you won’t have the cron log in the container, but the result of the command should be visible using `docker logs`, since cron.sh uses /dev/stdout.

But I don’t understand why this isn’t ‘fixed’ in some way:

- The default is AJAX, which is the ‘least reliable’ according to the documentation (why is this the default, then?)
- In the basic settings I can select cron, but this won’t activate cron (why make it an option there? Does this do anything?)
- The preferred solution, when using Docker, is to use crontab on the host (doesn’t this defy the point of Docker? What’s the problem with using cron in a container?)

---

<div class="post-metadata">

**Author:** ![jlgarnier](https://help.nextcloud.com/user_avatar/help.nextcloud.com/jlgarnier/32/14742_2.png) [@jlgarnier](https://help.nextcloud.com/u/jlgarnier)\
**Post date:** [October 5, 2021, 9:46am UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/17 "2021-10-05T09:46:50Z")

</div>

I fully agree and did the same analysis: why is Ajax the default setting and why doesn’t the Cron option just set the appropriate crontab?

By the way, I think the cron working was disturbed by the ‘listman’ plugin, which was looking for an unknown file and crashed during the task… I’ve disabled the plugin, hope this will fix the whole stuff!

Thanks again all for your help!

---

<div class="post-metadata">

**Author:** ![norsemangrey](https://help.nextcloud.com/user_avatar/help.nextcloud.com/norsemangrey/32/35914_2.png) [@norsemangrey](https://help.nextcloud.com/u/norsemangrey)\
**Post date:** [March 18, 2022, 12:59pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/19 "2022-03-18T12:59:16Z")

</div>

> [@Reiner\_Nippes](#):
>
> nevertheless after i wrote the comment above i found this article in german [https://www.projekt-rootserver.de/cron-events-in-docker-containern-zum-laufen-bringen/2019/09/](https://www.projekt-rootserver.de/cron-events-in-docker-containern-zum-laufen-bringen/2019/09/) it says: don’t do it with busybox. not a good idea. the authors advice: use the host cron + docker exec

Is it still the case that this approach is not recommended? It seems to be the suggested approach in the official [NextCloud docker repo](https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/insecure/mariadb/fpm/docker-compose.yml).

---

<div class="post-metadata">

**Author:** ![Reiner\_Nippes](https://help.nextcloud.com/user_avatar/help.nextcloud.com/reiner_nippes/32/9479_2.png) [@Reiner\_Nippes](https://help.nextcloud.com/u/Reiner_Nippes)\
**Post date:** [March 18, 2022, 6:35pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/20 "2022-03-18T18:35:47Z")

</div>

@norsemangrey I can’t tell you. But since it is “Nextcloud Offical” it can’t be wrong.

---

<div class="post-metadata">

**Author:** ![KarlF12](https://help.nextcloud.com/user_avatar/help.nextcloud.com/karlf12/32/46873_2.png) [@KarlF12](https://help.nextcloud.com/u/KarlF12)\
**Post date:** [March 23, 2022, 12:09pm UTC](https://help.nextcloud.com/t/docker-setup-cron/78547/21 "2022-03-23T12:09:54Z")

</div>

You can use the docker host’s cron to invoke cron.php in the container every 5 minutes. Simple and effective. I’ve done this on several of my installations and never had a problem with it.

I’ve used the official supervisord setup too and that also works.

[Next page](https://help.nextcloud.com/t/docker-setup-cron/78547.md?page=2)
