Verifying where to add cron jobs docker-compose + cron

Hi guys,

Host: proxmox
VM: Ubuntu server 18.04.5 LTS.
Nextcloud: 20.0.4
Everything updated

I run Nginx Proxy Manager as my reverse proxy also in a separate container.

I installed Nextcloud via the docker-compose example but I customized it of course… insecure/mariadb-cron-redis/apache/docker-compose.yml

My docker-compose file:

version: '3'

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

  redis:
    image: redis:alpine
    restart: always

  app:
    image: nextcloud:latest
    restart: always
    ports:
      - 8888:80
    volumes:
      - ./nextcloud:/var/www/html
      - /media/vdb/data:/var/www/html/data # RAIDZ1 Big storage
      - ./nextcloud/config:/var/www/html/config
      - ./nextcloud/apps:/var/www/html/apps
      - ./nextcloud/php.ini:/usr/local/etc/php/conf.d/zzz-custom.ini
    environment:
      - MYSQL_HOST=db
      - MYSQL_PASSWORD=XXXXXXXX
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - NEXTCLOUD_TRUSTED_DOMAINS=XXXXXXXX
      - REDIS_HOST=redis
      - NEXTCLOUD_ADMIN_USER=XXXXXXXX
      - NEXTCLOUD_ADMIN_PASSWORD=XXXXXXXX
    depends_on:
      - db
      - redis

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

I want to start expanding cron jobs to for example do preview building, calander sync etc. Everything is working like it should and every 5 min cron is working like it should (green light for background jobs Cron under basic settings).

Now the question is, were do I add extra cron rules/lines like for example:

0   0 * * 0 php -f /var/www/html/occ fulltextsearch:index

There is a cron.php file in my mapped volume .nextcloud:/var/www/html but when I look in to that is a full file with loads of text.

  GNU nano 2.9.3                                                                                                                         cron.php                                                                                                                                   

                // We only ask for jobs for 14 minutes, because after 5 minutes the next
                // system cron task should spawn and we want to have at most three
                // cron jobs running in parallel.
                $endTime = time() + 14 * 60;

                $executedJobs = [];
                while ($job = $jobList->getNext()) {
                        if (isset($executedJobs[$job->getId()])) {
                                $jobList->unlockJob($job);
                                break;
                        }

                        $job->execute($jobList, $logger);
                        // clean up after unclean jobs
                        \OC_Util::tearDownFS();

                        $jobList->setLastJob($job);
                        $executedJobs[$job->getId()] = true;
                        unset($job);

                        if (time() > $endTime) {
                                break;
                        }
                }
        } else {
                // We call cron.php from some website
                if ($appMode === 'cron') {
                        // Cron is cron :-P
                        OC_JSON::error(['data' => ['message' => 'Backgroundjobs are using system cron!']]);
                } else {
                        // Work and success :-)
                        $jobList = \OC::$server->getJobList();
                        $job = $jobList->getNext();
                        if ($job != null) {
                                $job->execute($jobList, $logger);
                                $jobList->setLastJob($job);
                        }
                        OC_JSON::success();
                }
        }

        // Log the successful cron execution
        $config->setAppValue('core', 'lastcron', time());
        exit();
} catch (Exception $ex) {
        \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
} catch (Error $ex) {
        \OC::$server->getLogger()->logException($ex, ['app' => 'cron']);
}

Or do I need to add - ./mycronfile:/var/spool/cron/crontabs/www-data to my compose file like suggested in:

and then add the lines i want to that new file?

Does the base line in that file: */5 * * * * php -f /var/www/html/cron.php call the cron.php file i was referring to? So if for example i want to change the interval I change /5 to /15 for a 15 min interval?

If you need more info, please met me know!

So i got a new command I want to add to cron, its from the preview generator app.

Important: To enable pre-generation of previews you must add php /var/www/nextcloud/occ preview:pre-generate to a system cron job that runs at times of your choosing.

Anyone? Would love some help guys before I destroy my setup…

it’s already in the article: use docker exec ... and put all jobs in the hosts crontab.

well. ok. i assume there is an equivalent to docker exec ... in proxmox.

But thats using the cronjob from the host and not the one in the nextcloud container.

1 Like

and?

there is no cron in a container. a container isn’t a virtual maschine. somewhere in the other thread is a link to a blob post explaining why it’s not a good idea to start cron in the container.

So you suggest I create a separate script on the host. And then call the script with crontab?

So example:

0   0 * * 0 php -f ????????? nextcloud_optimize.sh

Why not create a crontab and put the commands in directly? Any advantage of the script? For example:

0   0 * * 0 php -f docker exec --user www-data nextcloud php occ files:scan --all
0   0 * * 0 php -f docker exec --user www-data nextcloud php occ files:scan-app-data
0   0 * * 0 php -f docker exec --user www-data nextcloud php occ preview:pre-generate

if you use a single script or call each occ command individual in your cron is up to you and depends if they should run all at the same time or with different intervalls.

two thought about this:

0   0 * * 0 php -f docker exec --user www-data nextcloud php occ files:scan --all
0   0 * * 0 php -f docker exec --user www-data nextcloud php occ files:scan-app-data
0   0 * * 0 php -f docker exec --user www-data nextcloud php occ preview:pre-generate

all three job would run at exact the same time. that could cause a performance problem or memory shortage. if you put them i a script they would run subsequently, one after each other.

php -f docker exec that won’t work. php is a script language. with that command you try to run php on the host with a file “docker” as script. of course there is no docker file that could be interpreted as a php script.

but again i was talking about docker. you are running proxmox. you may better check the proxmox documetation how to run a cronjob inside a proxmox container.

1 Like

Thanks for the reply, sorry about my late reply back.

Maby my text was confusing. I run Proxmox as hypervisor, Run a Ubuntu-server VM and there i run Docker with Nextcloud. So for Nextcloud the host is Ubuntu server (not Proxmox).