Provide the ability to run user-defined cron tasks

Mount individual shell scripts (with the `*.sh` extension) inside `/cron-scripts/` in the
container. The cron daemon will automatically run these scripts inside the Nextcloud
container for you. Write the scripts as if they will run in the Nextcloud container
itself.
This commit is contained in:
Robert Dailey 2018-12-06 17:30:27 -06:00
parent a48d069b3e
commit ab6372fa79
5 changed files with 70 additions and 11 deletions

View file

@ -7,6 +7,7 @@ ENV NEXTCLOUD_PROJECT_NAME=
ENV NEXTCLOUD_CRON_MINUTE_INTERVAL=15
COPY scripts/*.sh /
COPY scripts/cron-scripts /cron-scripts
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -67,13 +67,57 @@ is checked every interval of the health check. If any of these checks fail, it i
container's health status will become *unhealthy*. In this case, you should restart the container.
1. The `crond` process must be running.
2. The Nextcloud container must be available and running. One important note here: When this
1. The Nextcloud container must be available and running. One important note here: When this
container starts up, it immediately searches for the container by name and remembers it by the
container's ID. If for whatever reason the Nextcloud container changes in such a way that the ID
is no longer valid, the health check would fail.
# Customizing Cron Tasks
This container provides the ability for you to run additional tasks inside the Nextcloud container
in addition to the default `cron.php` task. To add your custom tasks, follow these steps:
1. Write a shell script that runs the commands that will be part of your task. This shell script
must have the `.sh` extension. An example of the contents of such a script is below. As an
optional security measure, do not make this shell script executable. The contents of the file are
piped into `bash`, so the executable bit is not used.
```sh
#!/usr/bin/env bash
php -f /var/www/html/cron.php
```
1. Mount this shell script inside the `/cron-scripts` directory. Here's an example if you're using
`docker-compose.yml`:
```yml
services:
cron:
image: voidpointer/nextcloud-cronjob
volumes:
- ./my-scripts/do-something.sh:/cron-scripts/do-something.sh:ro
```
1. Recreate the container. Your script will now execute in the Nextcloud container at a regular
interval.
## Notes
* All cron task shell scripts run at the same interval defined by `NEXTCLOUD_CRON_MINUTE_INTERVAL`.
* Modification of your own shell scripts on the host do not require that you restart/recreate the
container.
# Debugging
All logs from `crond` are configured to print to stdout, so you can monitor container logs (via
`docker-compose logs -f`). This should allow you to make sure your cron job is working. You can also
use the "Overview" page in Nextcloud Settings to see if the cron job is being run regularly.
use the "Overview" page in Nextcloud Settings to see if the cron job is being run regularly. Here is
an example of the logs you will see:
```
Started crond
-------------------------------------------------------------
Executing Cron Tasks: Thu Dec 6 17:28:00 CST 2018
-------------------------------------------------------------
> Running Script: occ-preview-pre-generate.sh
> Running Script: run_cron_php.sh
> Done
```

View file

@ -0,0 +1,2 @@
#!/usr/bin/env bash
php -f /var/www/html/cron.php

View file

@ -1,5 +1,17 @@
#!/usr/bin/env bash
set -ex
set -e
containerName="$1"
docker exec "$containerName" php -f /var/www/html/cron.php
echo "-------------------------------------------------------------"
echo " Executing Cron Tasks: $(date)"
echo "-------------------------------------------------------------"
# Loop through all shell scripts and execute the contents of those scripts in the Nextcloud
# container. It's done this way so that the user may mount more scripts to be executed in addition
# to the default ones.
cd /cron-scripts
for script in *.sh; do
echo "> Running Script: $script"
docker exec -i "$1" bash < $script
done
echo "> Done"

View file

@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -ex
set -e
if [[ -z "$NEXTCLOUD_CONTAINER_NAME" ]]; then
echo "NEXTCLOUD_CONTAINER_NAME is a required variable"