Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background jobs: Cron does not run #1695

Closed
beryl03 opened this issue Feb 15, 2022 · 51 comments
Closed

Background jobs: Cron does not run #1695

beryl03 opened this issue Feb 15, 2022 · 51 comments
Labels

Comments

@beryl03
Copy link

beryl03 commented Feb 15, 2022

Tag: latest (2022/02/04)
When trying to setup Cron as the background job, it seems it cannot work without a lot of work on top of the image
Missing:
Packages: Cron (and dependencies)
Service: Something to run Crontab jobs
Settings to Reproduce:

  1. Install docker image from tag: latest
  2. Setup dependencies (SQL DB) and config to get NextCloud started
  3. Enable Cron background Job
  • Settings -> Basic Settings -> Select " Cron" Radio Button under "Background jobs" section
  1. Wait 5 minutes and reload page
  • observe that the last job was run over 5 minutes ago (PROBLEM)
  1. Log into docker container terminal as Root
  2. Install Cron package and dependencies
  3. Install editor and configure EDITOR env var (I used nano)
  4. edit crontab (crontab -u www-data -e) to add recommended line per documentation (https://docs.nextcloud.com/server/23/admin_manual/configuration_server/background_jobs_configuration.html)
  5. Wait 5 minutes and reload page
  • observe that the last job was run over 5 minutes ago (PROBLEM)
  1. Further investigation you can see that there are no Cron logs at expected locations, which indicates Cron is not even running. I was unable to figure out how to start the Cron service since the docker image is stripped down version of Debian. SystemD does not seem to be installed.

Mitigation:
Mitigation is to setup Cron on Host and run "docker exec -u 33 -t nextcloud-app php -f /var/www/html/cron.php" (www-data UID = 33)

  • Note: on Fedora Server, Cron is not enabled/running after Cron is installed. the service has to be enabled for Cron jobs to run (systemctl enable crond.service). I assume something similar needs to be done within the docker image/container.

Proposed Solution:

  1. Create Docker Image with cron installed and setup such that selecting Cron as a background job is enough to setup the cron job.
    OR
  2. Update Documentation to note that official docker image[s] do not support Cron Jobs in a Docker Container and list links to solutions or mitigations.
@beryl03
Copy link
Author

beryl03 commented Feb 15, 2022

Here is some info from docker image inspect for image versioning information:

"Id": "sha256:df82fda354851c092a2e95bb49189231931c357c36ab03a4ec3c19fdd64c4925",
"RepoTags": [
"nextcloud:latest"
],
"RepoDigests": [
"nextcloud@sha256:867d3f34d5a7baa1fb48b18b86e36486e406832881219c5bcd7f9952bc947366"
],

@Karem-sobhy
Copy link

Karem-sobhy commented Feb 17, 2022

The background cron should be run in a separate container
If you provide the docker-compose.yml you using I can help you with how to set up the cron container.
Or the docker compose examples right here can help you.
https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/insecure/mariadb/apache/docker-compose.yml
It should be like that

app:
  image: nextcloud:apache
  restart: always
  ports:
    - 127.0.0.1:8080:80
  volumes:
    - nextcloud:/var/www/html
  environment:
    - MYSQL_HOST=db
    - REDIS_HOST=redis
  env_file:
    - db.env
  depends_on:
    - db
    - redis

cron:
  image: nextcloud:apache
  restart: always
  volumes:
    - nextcloud:/var/www/html
  entrypoint: /cron.sh
  depends_on:
    - db
    - redis

@beryl03
Copy link
Author

beryl03 commented Feb 17, 2022

That could work but if it is starting a while new container, that sounds like a bit more RAM just to run Cron.php. also seeing two containers sharing the volume seems odd. Since you could have file IO errors.
If this is the recommended way, there was no mention of this method in any of the documentation (docs.nextcloud.com).

@martadinata666
Copy link

so few ways to use the cron is,

  • use separate compose like above
  • use host cron and docker exec command
  • custom image to run cron apa apache in parallel

@Karem-sobhy
Copy link

Karem-sobhy commented Feb 17, 2022

That could work but if it is starting a while new container, that sounds like a bit more RAM just to run Cron.php. also seeing two containers sharing the volume seems odd. Since you could have file IO errors. If this is the recommended way, there was no mention of this method in any of the documentation (docs.nextcloud.com).

That's the recommended way in the examples and no proplem with sharing the same volume because it's only calls the cron file and about ram it uses 38mb of ram so not really a problem so don't worry and spin that extra cron container

Edit..
You don't need to add all volumes only the
/var/www/html

@beryl03
Copy link
Author

beryl03 commented Feb 17, 2022

@martadinata666 , I use the host to push a docker exec, and it works, and cron initiates sending the command to the container.
With regards to all options, I did not find any documentation stating the docker image crontab does not work and steps to take to fix it. I think it would be easiest to add notes to the documentation to state such and list solutions and links.
Documentation locations I am referring to:
https://github.com/nextcloud/docker/wiki
https://docs.nextcloud.com/server/23/admin_manual/configuration_server/background_jobs_configuration.html

@Karem-sobhy
Copy link

@martadinata666 , I use the host to push a docker exec, and it works, and cron initiates sending the command to the container. With regards to all options, I did not find any documentation stating the docker image crontab does not work and steps to take to fix it. I think it would be easiest to add notes to the documentation to state such and list solutions and links. Documentation locations I am referring to: https://github.com/nextcloud/docker/wiki https://docs.nextcloud.com/server/23/admin_manual/configuration_server/background_jobs_configuration.html

But it's documented in the official examples here
https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/insecure/mariadb/apache/docker-compose.yml

@beryl03
Copy link
Author

beryl03 commented Feb 17, 2022

@kemoantemo ,
I am not saying what you linked is bad. I am saying me as a user never found that in all the searching I did. What good is "documentation" if a user cannot find the "documentation" to find the answer?
What I am asking is if we can help users (like me) find the information we need by putting the documentation or links to documentation where a [new] user would look.

@Karem-sobhy
Copy link

I get you but any new user should follow the examples of the official docker so where is the problem? Maybe it's not in the documentation of nextcloud because next cloud have a lot of deploy methods so you should check the examples of the method you going to use and don't worry from the cron container it's not fully nextcloud container it doesn't run nextcloud instance it just run a cron process that calls cron.php every 5 minutes so it's pretty good and easy to applicate solution. you can use the supervisored to run nextcloud and cron from same container but it's really almost the same because after all it's the same PID so just use the official examples and don't worry

@martadinata666
Copy link

martadinata666 commented Feb 18, 2022

everyone got personal preferences, in my case i rather not modify host so i create custom image with added supervisor to run cron and apache in parallel. Because i also need additional job like full text search, and preview generator, and pretty messed up on upgrade as it get overwritten cmiiw

@danieljkemp
Copy link

The NC docker docs just mention cron briefly, pointing to the examples:

The examples folder gives a few examples on how to add certain functionalities, like including the cron job, smb-support or imap-authentication.

Here is where cron is defined in the examples

This would be the correct way to do it with docker-compose*. Containers should be a single process to allow logs and error handling to make sense.

Since almost every(?) instance needs cron, maybe it should be more prominent in the docs?

*(In Kubernetes you can use a CronJob to handle the cron script which feels somewhat nicer.)

@keunes
Copy link

keunes commented Mar 22, 2022

Sorry to revive this a month later, but I am also facing issues with my Cron set-up.

My docker-compose.yml
version: '2'

services:
  db:
    image: mariadb:10.5
    restart: always
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=[pw]
      - MYSQL_PASSWORD=[pw]
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  redis:
    image: redis:alpine
    restart: always

  app:
    image: nextcloud
    restart: always
    ports:
      - 80:80
    depends_on:
      - db
    links:
      - redis
    volumes:
      - ./config:/var/www/html/config
      - ./apps:/var/www/html/custom_apps
      - /nextcloud-data:/var/www/html/data
    environment:
      - MYSQL_PASSWORD=[pw]
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db
      - SMTP_HOST=mail.me.eu
      - SMTP_SECURE=tls
      - SMTP_PORT=587
      - [email protected]
      - SMTP_PASSWORD=[pw]
      - MAIL_DOMAIN=me.eu
      - MAIL_FROM_ADDRESS=system
      - TRUSTED_PROXIES=192.xxx.xxx.xx
      - TRUSTED_DOMAIN=cloud.me.eu
      - REDIS_HOST=redis

  cron:
    image: nextcloud
    restart: always
    volumes:
      - ./config:/var/www/html/config
      - ./apps:/var/www/html/custom_apps
      - /nextcloud-data:/var/www/html/data
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

Despite copying the proposed cron container description - except the volumes as I'm only using more specific ones rather than just the generic /var/www/html. However, docker doesn't seem to do anything. I am still seeing the notification in Nextcloud admin pages that the 'last executed job was yesterday'.

I asked support on the forum but unfortunately the answers provided there don't really get me much further. On the one hand I'm hoping to get feedback on my situation. On the other hand I'm here to support the call for more documentation/explanation on using docker (more than only an example file that doesn't explain anything and isn't of much help when debugging).

@Karem-sobhy
Copy link

@keunes the /var/www/html is an essential for not just cron but also for upgrading you should have it first then take every custom data or config as you want after so try to make a volume for /var/www/html
If you need help you can email me at [email protected] I'd love to help

@danieljkemp
Copy link

@keunes try setting the MYSQL variables in the environment of the cron container as well. They're not specifying any in the example, but I am going to guess that is because they are using defaults.

@Karem-sobhy
Copy link

@danieljkemp it's getting it from the config file no need to he just need to mount the /var/www/html

@keunes
Copy link

keunes commented Mar 23, 2022

@keunes the /var/www/html is an essential for not just cron but also for upgrading you should have it first then take every custom data or config as you want after so try to make a volume for /var/www/html

You're amazing, this worked! Just for my understanding: why is it that adding this 'parent' volume fixed it? Which directories/files are there that cron needs (or that the app needs to know that cron is running)?

I think it would be helpful to mention in the docs (docker Readme) what you told me also, namely that the parent folder must be there, and that child directories may be specified after. I see now that it is mentioned Persistent data somewhat implicitly, but not mentioned in the Docker compose section. I'll prepare a PR with some suggested changes later on.

Also, another question for my understanding: what happens behind the scenes when mounting a parent and then some child directories? On the first run with the global/first volume (when the nextcloud volume doesn't exist yet), does it work like this?

  • first main volume created (replicating the files & structure from the image in the virtual file system), after which
  • any other volume I defined is mounted in the 'parent'/first volume (for which I suppose the original files that were taken from the image are removed first)

@martadinata666
Copy link

The essential thing is the /var/www/html in your cron and app need to be inline, if it run separately, the data generated from cron won't available / written to /var/www/html in app. Dunno exactly what is it written or read, /var/www/html/cron.php if you love investigate further.

Assume you had /var/www/html called it base and you also had /var/www/html/config defined called override, both had /var/www/html/config. Container will read from the override not from the base.

@danieljkemp
Copy link

The crazy part is that /var/www/html/ contains the php source of the app. It is actually copying the application source to a volume when the main container updates, then keeping it a volume.

You'd expect to just need to mount the config and data directories into the app containers (main and cron included), and this is indeed how other applications work, but no, the the nextcloud containers references that volume (the one containing just copied source) directly.

@Karem-sobhy
Copy link

@keunes the /var/www/html is an essential for not just cron but also for upgrading you should have it first then take every custom data or config as you want after so try to make a volume for /var/www/html

You're amazing, this worked! Just for my understanding: why is it that adding this 'parent' volume fixed it? Which directories/files are there that cron needs (or that the app needs to know that cron is running)?

I think it would be helpful to mention in the docs (docker Readme) what you told me also, namely that the parent folder must be there, and that child directories may be specified after. I see now that it is mentioned Persistent data somewhat implicitly, but not mentioned in the Docker compose section. I'll prepare a PR with some suggested changes later on.

Also, another question for my understanding: what happens behind the scenes when mounting a parent and then some child directories? On the first run with the global/first volume (when the nextcloud volume doesn't exist yet), does it work like this?

  • first main volume created (replicating the files & structure from the image in the virtual file system), after which
  • any other volume I defined is mounted in the 'parent'/first volume (for which I suppose the original files that were taken from the image are removed first)

The reason that cron didn't work is that the /var/www/html is just empty because you didn't call the entrypoint.sh which checks the version of your nextcloud and if it's existing or not then copy from /usr/src/nextcloud as i remember not sure. So that does happen only if you started the entrypoint.sh so that you don't have problems with the app container but it's surely slow at starting because it copies everytime it starts and for the cron container it doesn't find the php source of nextcloud at /var/www/html so it can't run. I hope I managed to describe it in a good way.

@keunes
Copy link

keunes commented Apr 9, 2022

Thanks all for the help and explanations (and sorry for replying only now). I managed to get it working! I think indeed the trick was to add the volume nextcloud:/var/www/html both to app and cron.

@J0WI J0WI added the question label Apr 27, 2022
@ninadpchaudhari
Copy link

ninadpchaudhari commented May 9, 2022

@danieljkemp it's getting it from the config file no need to he just need to mount the /var/www/html

Thanks for that info, it answered my question, hope it helps someone else too.
Quesion:
since the config file needs to be retrieved from /var/www/html, why don't we just make the cron service's depends_on parameter to be on app instead of db & redis ? Sounds this could be a good Pull Request?

I feel, from the current example, if it only depends on DB, then we gotta give it all the environment parameters! Which is not really great thing to do.

@Karem-sobhy
Copy link

Quesion: since the config file needs to be retrieved from /var/www/html, why don't we just make the cron service's depends_on parameter to be on app instead of db & redis ? Sounds this could be a good Pull Request?

depends_on only makes the containers start after the dependency containers start what you want is

volumes_from:
     - app

and I did a pull request #1710 for it but it doesn't work with stack so they didn't implement it

@persching1978
Copy link

persching1978 commented Jul 17, 2022

Hello to all,
I've the same problems, but adding the nextcloud:apache to my docker-compose.yml did not solve the problem! I've a Synology DS 220+ and installed portainer to do all the docker installations. In this installation I've to adapt the volume part to the right path of my synology. But I think this is the only modifcation.
Nextcloud is working for a few weeks, but I've always the error message with cron, so I try to fix this...

Here is my portainer stack (I think it's nearly the same as the docker-compose.yml):

`services:
db:
image: mariadb:latest
container_name: mariadb
restart: always
ports:
- 3306:3306
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW --innodb-file-per-table=1 --skip-innodb-read-only-compressed
volumes:
- /volume1/docker/nextcloud_db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- TZ=Europe/Berlin
networks:
- web

redis:
image: redis:latest
restart: always
networks:
- web
volumes:
- /volume1/docker/redis:/var/lib/redis

app:
image: nextcloud:latest
container_name: nextcloud
restart: always
links:
- db
volumes:
- /volume1/docker/nextcloud:/var/www/html
- /volume1/docker/configfiles/nextcloud/.htaccess:/var/www/html/.htaccess
logging:
driver: "json-file"
options:
max-size: 10m
max-file: "3"
environment:
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_HOST=db
- REDIS_HOST=redis
- TZ=Europe/Berlin
networks:
- web
depends_on:
- db
- redis

cron:
image: nextcloud:apache
restart: always
volumes:
- /volume1/docker/nextcloud:/var/www/html
entrypoint: /cron.sh
networks:
- web
depends_on:
- app
- db
- redis

networks:
web:
external: true`

@persching1978
Copy link

It's fantastic: after posting my comment I've rebooted my Diskstation and now nextcloud cron works fine. :)

@lawipac
Copy link

lawipac commented Jul 28, 2022

Personally, I think docker exec is a better way, because is does not requires to change official nextcloud docker image, and automatic upgrade is possible.

@wekaz
Copy link

wekaz commented Aug 19, 2022

The background cron should be run in a separate container If you provide the docker-compose.yml you using I can help you with how to set up the cron container. Or the docker compose examples right here can help you. https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/insecure/mariadb/apache/docker-compose.yml It should be like that

app:
  image: nextcloud:apache
  restart: always
  ports:
    - 127.0.0.1:8080:80
  volumes:
    - nextcloud:/var/www/html
  environment:
    - MYSQL_HOST=db
    - REDIS_HOST=redis
  env_file:
    - db.env
  depends_on:
    - db
    - redis

cron:
  image: nextcloud:apache
  restart: always
  volumes:
    - nextcloud:/var/www/html
  entrypoint: /cron.sh
  depends_on:
    - db
    - redis

mine doesn't work. i check the container log for the cron service and here's what i found:

cron_1         | crond: file www-data:
cron_1         | crond:  line php -f /var/www/html/cron.php
cron_1         | crond: wakeup dt=60
cron_1         | crond: file www-data:
cron_1         | crond:  line php -f /var/www/html/cron.php
cron_1         | crond:  job: 0 php -f /var/www/html/cron.php
cron_1         | crond: child running /bin/bash
cron_1         | crond: USER www-data pid  17 cmd php -f /var/www/html/cron.php
cron_1         | Exception: Not installed in /var/www/html/lib/base.php:282
cron_1         | Stack trace:
cron_1         | #0 /var/www/html/lib/base.php(663): OC::checkInstalled(Object(OC\SystemConfig))
cron_1         | #1 /var/www/html/lib/base.php(1104): OC::init()
cron_1         | #2 /var/www/html/cron.php(43): require_once('/var/www/html/l...')
cron_1         | #3 {main}
cron_1         | crond: wakeup dt=10

for reference, this is my docker-compose:

version: '3'

services:

  proxy:
    image: jwilder/nginx-proxy:alpine
    labels:
      - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
    container_name: nextcloud-proxy
    networks:
      - nextcloud_network
    ports:
      - 24280:80
      - 24443:443
    volumes:
      - ./proxy/conf.d:/etc/nginx/conf.d:rw
      - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
      - ./proxy/html:/usr/share/nginx/html:rw
      - ./proxy/certs:/etc/nginx/certs:ro
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/tmp/docker.sock:ro
    environment:
      - upload_max_filesize=2048M
    restart: unless-stopped

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: nextcloud-letsencrypt
    depends_on:
      - proxy
    networks:
      - nextcloud_network
    volumes:
      - ./proxy/certs:/etc/nginx/certs:rw
      - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
      - ./proxy/html:/usr/share/nginx/html:rw
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    restart: unless-stopped

  db:
    image: mariadb
    container_name: nextcloud-mariadb
    networks:
      - nextcloud_network
    volumes:
      - ./db:/var/lib/mysql
      - /etc/localtime:/etc/localtime:ro
    environment:
      - MYSQL_ROOT_PASSWORD=*****************
      - MYSQL_PASSWORD=*****************
      - MYSQL_DATABASE=*****************
      - MYSQL_USER=*****************
    restart: unless-stopped

  app:
    image: nextcloud:latest
    container_name: nextcloud-app
    networks:
      - nextcloud_network
    depends_on:
      - letsencrypt
      - proxy
      - db
    volumes:
      - ./nextcloud_html:/var/www/html
      - ./app/config:/var/www/html/config
      - ./app/custom_apps:/var/www/html/custom_apps
      - ./app/data:/var/www/html/data
      - ./app/themes:/var/www/html/themes
      - /etc/localtime:/etc/localtime:ro
    environment:
      - VIRTUAL_HOST=*****************
      - LETSENCRYPT_HOST=*****************
      - LETSENCRYPT_EMAIL=*****************
      - PHP_MEMORY_LIMIT=2048M
      - PHP_UPLOAD_LIMIT=2048M
    restart: unless-stopped

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - ./nextcloud_html:/var/www/html
    entrypoint: /cron.sh
    networks:
      - nextcloud_network
    depends_on:
      - letsencrypt
      - proxy
      - db

volumes:
  nextcloud:
  db:

networks:
  nextcloud_network:

@martadinata666
Copy link

The nextcloud volumes and cron volumes must inline

@wekaz
Copy link

wekaz commented Aug 19, 2022

The nextcloud volumes and cron volumes must inline

already did

- ./nextcloud_html:/var/www/html

@martadinata666
Copy link

The nextcloud volumes and cron volumes must inline

already did

- ./nextcloud_html:/var/www/html

The config? The data? The custom?

@wekaz
Copy link

wekaz commented Aug 20, 2022

so i have to add these too?

      - ./nextcloud_html:/var/www/html
      - ./app/config:/var/www/html/config
      - ./app/custom_apps:/var/www/html/custom_apps
      - ./app/data:/var/www/html/data
      - ./app/themes:/var/www/html/themes
      - /etc/localtime:/etc/localtime:ro

@martadinata666
Copy link

so i have to add these too?

      - ./nextcloud_html:/var/www/html
      - ./app/config:/var/www/html/config
      - ./app/custom_apps:/var/www/html/custom_apps
      - ./app/data:/var/www/html/data
      - ./app/themes:/var/www/html/themes
      - /etc/localtime:/etc/localtime:ro

Yes, because /var/www/html assuming that everything at that path, if you had separate mount for that then you also need to mount it too

@wekaz
Copy link

wekaz commented Aug 20, 2022

yes, finally it works. thank you

@megatonix
Copy link

for me cron is not working too, i don't know what to do anymore.
Here is my docker-compose:

`version: '3'

volumes:
nextcloud:
db:

networks:
nextcloud_network:
external: false

services:
db:
image: mariadb:10.5
restart: always
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
volumes:
- db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_PASSWORD=
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
networks:
- nextcloud_network

app:
image: nextcloud
restart: unless-stopped
ports:
- 8080:80
links:
- db
volumes:
- nextcloud:/var/www/html

environment:
  - MYSQL_PASSWORD=
  - MYSQL_DATABASE=
  - MYSQL_USER=
  - MYSQL_HOST=db
  - REDIS_HOST=redis
networks:
  - nextcloud_network

cron:
image: nextcloud:apache
restart: always
volumes:
- nextcloud:/var/www/html
entrypoint: /cron.sh
depends_on:
- db
- redis

redis:
image: redis:alpine
container_name: redis
volumes:
- /docker/nextcloud/redis:/data
networks:
- nextcloud_network
restart: unless-stopped`

Cron

@Chimera20
Copy link

Found this page while looking up the same issue. After much trial and error, I found there is no need for an extra Container just to run Cron. The host OS is absolutely fine to run this task. However, what I found does contradict the Nextcloud documentation a bit.

Running cron through crontab -u www-data -e never quite worked for me. That could be a "me" problem as I missed something.

Solution for me:
Edit the /etc/crontab file directly on the host OS, so the cron job becomes a system wide default but, still specify the user, and don't make docker exec interactive. Drop the -it

My host OS /etc/crontab
*/5 * * * * root /usr/bin/docker exec -u www-data nextcloud php -f /var/www/html/cron.php

Since this, I have had the little green light of joy showing on the web GUI for NC

@phatvnvc
Copy link

phatvnvc commented Oct 7, 2022

here is my yml file compose , please help me to run cron yml

version: '3.7'

networks:
vnvc-network:
driver: bridge

volumes:
disk-data:
driver_opts:
device: /home/nextcloud/db/
type: none
o: bind
disk-app:
driver_opts:
device: /home/nextcloud/app/
type: none
o: bind

services:
mariadb:
image: mariadb
container_name: mariadb
restart: always
volumes:
- disk-data:/var/lib/mysql
networks:
- vnvc-network

ports:

- 27017:27017

environment:
  - MYSQL_ROOT_PASSWORD=Vnvc@#HVT198
  - MYSQL_PASSWORD=Vnvc@HVT198
  - MYSQL_DATABASE=nextcloud
  - MYSQL_USER=nextcloud

nextcloud:
image: nextcloud
container_name: nextcloud
restart: always
volumes:
- disk-app:/var/www/html
networks:
- vnvc-network
ports:
- 8888:80
environment:
- MYSQL_PASSWORD=Vnvc@HVT198
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
- MYSQL_HOST=mariadb
depends_on:
- mariadb

Thanks a lots

@jjasoncool
Copy link

jjasoncool commented Jan 16, 2023

Dear all,

you can build your image using below dockerfile

@beryl03 maybe you can try this

ARG NC_VERSION
FROM nextcloud:${NC_VERSION}

RUN touch /var/log/cron.log
CMD /cron.sh >> /var/log/cron.log 2>&1 & php-fpm

Then docker-compose example below:

version: '3.9'

services:
  nextcloud:
    # "context" and "dockerfile" fields have to be under "build"
    build: 
      context: .
      dockerfile: <Directory>/Dockerfile

@Glideh
Copy link

Glideh commented Jan 26, 2023

so i have to add these too?

      - ./nextcloud_html:/var/www/html
      - ./app/config:/var/www/html/config
      - ./app/custom_apps:/var/www/html/custom_apps
      - ./app/data:/var/www/html/data
      - ./app/themes:/var/www/html/themes
      - /etc/localtime:/etc/localtime:ro

I would use volumes_from to make sure every volumes are there

  cron:
    image: nextcloud:${VERSION}
    volumes_from:
      - nextcloud
    entrypoint: /cron.sh

@bill88t
Copy link

bill88t commented Mar 13, 2023

My solution:
In docker-compose:

entrypoint: /bin/bash
command: -c "/nc-autocron.sh & /entrypoint.sh apache2-foreground"
volumes:
    - /path-to/nc-autocron.sh:/nc-autocron.sh:ro

nc-autocron.sh:

#!/bin/bash

echo "nc-autocron: Waiting 20s for instance to init.." >> /proc/1/fd/1
sleep 20

while true; do
    echo "nc-autocron: Running cron.php" >> /proc/1/fd/1
    php -f /var/www/html/cron.php
    sleep 50
done

@dormancygrace
Copy link

My solution: In docker-compose:

entrypoint: /bin/bash
command: -c "/nc-autocron.sh & /entrypoint.sh apache2-foreground"
volumes:
    - /path-to/nc-autocron.sh:/nc-autocron.sh:ro

nc-autocron.sh:

#!/bin/bash

echo "nc-autocron: Waiting 20s for instance to init.." >> /proc/1/fd/1
sleep 20

while true; do
    echo "nc-autocron: Running cron.php" >> /proc/1/fd/1
    php -f /var/www/html/cron.php
    sleep 50
done

I'm getting "/bin/bash: line 1: /nc-autocron.sh: Permission denied"

@bill88t
Copy link

bill88t commented Mar 25, 2023

Did you chmod +x the autocron?

@dormancygrace
Copy link

Did you chmod +x the autocron?

thx

@dormancygrace
Copy link

Did you chmod +x the autocron?

now there is anothe problem:

nc-autocron: Running cron.php
Console has to be executed with the user that owns the file config/config.php
Current user id: 0
Owner id of config.php: 33

@bill88t
Copy link

bill88t commented Mar 26, 2023

Well it seems you aren't setting the user directive in your compose.
For me having it all forced to my uid worked fine.
Perhaps by using sh -c it would work without setting the uid.
But this issue is not tech support.

@jjasoncool
Copy link

jjasoncool commented Apr 7, 2023

Dear all,

you can build your image using below dockerfile

@beryl03 maybe you can try this

ARG NC_VERSION
FROM nextcloud:${NC_VERSION}

RUN touch /var/log/cron.log
CMD /cron.sh >> /var/log/cron.log 2>&1 & php-fpm

Then docker-compose example below:

version: '3.9'

services:
  nextcloud:
    # "context" and "dockerfile" fields have to be under "build"
    build: 
      context: .
      dockerfile: <Directory>/Dockerfile

I have found better settings from source code.

please ref:
https://github.com/nextcloud/docker/tree/master/.examples/dockerfiles/cron/fpm

use the supervisord is stable than modify dockerfile entrypoint.

@J0WI Maybe this issue can be closed?

@alexdatur
Copy link

alexdatur commented Apr 7, 2023

installed nc. postgre. redis through compose. all work, but try cron from host and see a strange error: /usr/bin/docker exec -u www-data nextcloud php -f /var/www/html/cron.php
Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] could not translate host name "postgres-nextcloud" to address: Temporary failure in name resolution in /var/www/html/lib/private/DB/Connection.php:142"

Can somebody help? im stuck . in past i use this in NC 25, but on fresh install it dont work. containers connects ok, all functions work, but cron - no

My compose file:
version: "2.1"

services:
nextcloud:
image: nextcloud:26.0.0-apache
container_name: nextcloud
environment:
- PUID=1000
- PGID=100
- TZ=Europe/Moscow
- REDIS_HOST=redis-nextcloud
- REDIS_HOST_PASSWORD=
volumes:
- /srv/dev-disk-by-uuid-201d48fb-9acc-4ebd-ac77-4a48206a723a/dockerimg/nextcloud/nextcloud_back/apps/:/var/www/html/apps
- /srv/dev-disk-by-uuid-201d48fb-9acc-4ebd-ac77-4a48206a723a/dockerimg/nextcloud/nextcloud_back/config/:/var/www/html/config
- /srv/dev-disk-by-uuid-201d48fb-9acc-4ebd-ac77-4a48206a723a/dockerimg/nextcloud/nextcloud_back/data/:/var/www/html/data
- /srv/dev-disk-by-uuid-201d48fb-9acc-4ebd-ac77-4a48206a723a/dockerimg/nextcloud/nextcloud_back/:/var/www/html
- /srv/dev-disk-by-uuid-ea7dbf22-5c45-4f9b-acdd-0a85cdd3c60c/:/mnt/data:rw
- ./nextcloud.ini:/usr/local/etc/php/conf.d/nextcloud.ini
ports:
- 177:80
restart: unless-stopped
depends_on:
- postgres-nextcloud
- redis-nextcloud

postgres-nextcloud:
image: postgres:15.2-alpine
container_name: postgres-nextcloud
restart: unless-stopped
environment:
- POSTGRES_DB=nextcloud
- POSTGRES_USER=nextcloud
- POSTGRES_PASSWORD=
volumes:
- /srv/dev-disk-by-uuid-201d48fb-9acc-4ebd-ac77-4a48206a723a/dockerimg/nextcloud/DB:/var/lib/postgresql/data

redis-nextcloud:
image: redis:alpine
container_name: redis-nextcloud
command: redis-server --requirepass
restart: unless-stopped

@J0WI
Copy link
Contributor

J0WI commented Apr 7, 2023

This is more or less a duplicate of #627.

@J0WI J0WI closed this as completed Apr 7, 2023
@almgwary
Copy link

working solution

https://stackoverflow.com/questions/76087354/how-to-fix-nextclouds-cron-job-seems-not-to-be-working/76087355#76087355

enter image description here

Here is a working solution assuming you've installed cron apt-get install cron

  1. As root user run
crontab -e
  1. Append this line which will run php script as www-data user
* * * * * su www-data -s /bin/bash -c "/usr/local/bin/php /var/www/html/cron.php"
  1. Restart cron service
 /etc/init.d/cron restart

@baderk
Copy link

baderk commented Jul 13, 2023

I almost tried what was suggested in this thread, but this is what worked for me.

I have Nextcloud running in a docker container, so I wanted to try making it work without spinning a separate container just to execute the cron.php file.

On the system's root crontab, I added this line and that's it. I didn't need to adjust anything else. My system is executing the docker exec command and specifying the www-data user to be used.

*/5 * * * * /usr/bin/docker exec -u www-data nextcloud php cron.php

@almgwary's way works if you have nextcloud running not inside docker container. But since the nextcloud's official container doesn't come with crontab, I have to use the system's crontab and docker exec command to achieve this.

@akmail
Copy link

akmail commented Apr 26, 2024

maybe I'm not getting it. but shouldn't nextcloud docker image bring its own predefined cron setting? why do I have to find out that my jobs are not running? and trigger the cron.php from outside? this is not how it should be! I'm disappointed meanwhile about the amount of effort that I had to put into my NC installation to make it work

@loveisfoss
Copy link

loveisfoss commented May 2, 2024

I almost tried what was suggested in this thread, but this is what worked for me.

I have Nextcloud running in a docker container, so I wanted to try making it work without spinning a separate container just to execute the cron.php file.

On the system's root crontab, I added this line and that's it. I didn't need to adjust anything else. My system is executing the docker exec command and specifying the www-data user to be used.

*/5 * * * * /usr/bin/docker exec -u www-data nextcloud php cron.php

@almgwary's way works if you have nextcloud running not inside docker container. But since the nextcloud's official container doesn't come with crontab, I have to use the system's crontab and docker exec command to achieve this.

This method also works for me but the command needs about 14 minutes in order to complete in my Raspberry Pi. One of the CPU cored is at 100% during this time and the temperature rises. If I run the command every 5 minutes, this means that 3 instances of the command are going to be running simultaneously at some point (occupying 3 CPU cores).

Is this normal?

EDIT: It seems like after running docker exec -u www-data nextcloud ./occ files:scan-app-data the command docker exec -u www-data nextcloud php -f /var/www/html/cron.php is being executed within seconds, after the first time. Let's see if this keeps being this way.

@hanisntsolo
Copy link

The background cron should be run in a separate container If you provide the docker-compose.yml you using I can help you with how to set up the cron container. Or the docker compose examples right here can help you. https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/insecure/mariadb/apache/docker-compose.yml It should be like that

app:
  image: nextcloud:apache
  restart: always
  ports:
    - 127.0.0.1:8080:80
  volumes:
    - nextcloud:/var/www/html
  environment:
    - MYSQL_HOST=db
    - REDIS_HOST=redis
  env_file:
    - db.env
  depends_on:
    - db
    - redis

cron:
  image: nextcloud:apache
  restart: always
  volumes:
    - nextcloud:/var/www/html
  entrypoint: /cron.sh
  depends_on:
    - db
    - redis

This worked for me updating the entrypoint to /cron.sh did really work I tried many other things but nothing worked. adding cron.sh worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests