diff --git a/README.md b/README.md
index e2a7dc7..9387249 100644
--- a/README.md
+++ b/README.md
@@ -53,16 +53,16 @@ entirely.
the service, if `NEXTCLOUD_PROJECT_NAME` is specified).
* `NEXTCLOUD_PROJECT_NAME`
- The name of the project if you're using Docker Compose. The name of
- the project, by default, is the name of the context directory you ran your `docker-compose.yml`
- from. This helps to build a "hint" used to identify the Nextcloud container by name. The hint is
- built as:
+ The name of the project if you're using Docker Compose. The name of the project, by default, is
+ the name of the context directory you ran your `docker-compose.yml` from. This helps to build a
+ "hint" used to identify the Nextcloud container by name. The hint is built as:
- ${NEXTCLOUD_PROJECT_NAME}_${NEXTCLOUD_CONTAINER_NAME}
+ ```txt
+ ${NEXTCLOUD_PROJECT_NAME}_${NEXTCLOUD_CONTAINER_NAME}
+ ```
* `NEXTCLOUD_CRON_MINUTE_INTERVAL`
- The interval, in minutes, of how often the cron task
- executes. The default is 15 minutes.
+ The interval, in minutes, of how often the cron task executes. The default is 15 minutes.
* `NEXTCLOUD_EXEC_USER`
The user that should be used to run the cron tasks inside the Nextcloud container. This parameter
@@ -79,10 +79,12 @@ 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
- 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.
+2. The Nextcloud container must be available and running.
+
+Because the Nextcloud container can be restarted while the the cronjob container is running, its
+container ID is not cached. Each time the cron task executes, it searches for the ID of the
+container. This ensures that even if you restart the Nextcloud container, the cronjob container will
+always function.
## Customizing Cron Tasks
@@ -99,7 +101,7 @@ in addition to the default `cron.php` task. To add your custom tasks, follow the
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
+2. Mount this shell script inside the `/cron-scripts` directory. Here's an example if you're using
`docker-compose.yml`:
```yml
@@ -110,7 +112,7 @@ in addition to the default `cron.php` task. To add your custom tasks, follow the
- ./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
+3. Recreate the container. Your script will now execute in the Nextcloud container at a regular
interval.
### Notes
diff --git a/scripts/cron-tasks.sh b/scripts/cron-tasks.sh
index 13ca88d..f10fe18 100755
--- a/scripts/cron-tasks.sh
+++ b/scripts/cron-tasks.sh
@@ -11,13 +11,22 @@ if [[ -n "$NEXTCLOUD_EXEC_USER" ]]; then
exec_user="--user $NEXTCLOUD_EXEC_USER"
fi
+# Obtain the ID of the container. We do this each iteration since the Nextcloud container may be
+# recreated while the cron container is still running. We will need to check for a new container ID
+# each time.
+containerId="$(/find-container.sh)"
+if [[ -z "$containerId" ]]; then
+ echo "ERROR: Unable to find the Nextcloud container"
+ exit 1
+fi
+
# 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 $exec_user -i "$1" bash < $script
+ docker exec $exec_user -i "$containerId" bash < $script
done
echo "> Done"
diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh
index cc2d028..d2a23e1 100755
--- a/scripts/entrypoint.sh
+++ b/scripts/entrypoint.sh
@@ -6,25 +6,7 @@ if [[ -z "$NEXTCLOUD_CONTAINER_NAME" ]]; then
exit 1
fi
-if [[ ! -z "$NEXTCLOUD_PROJECT_NAME" ]]; then
- containerName="${NEXTCLOUD_PROJECT_NAME}_"
-else
- matchEnd=","
-fi
-
-containerName="${containerName}${NEXTCLOUD_CONTAINER_NAME}"
-
-# Get the ID of the container so we can exec something in it later
-export containerId=$(/find-container.sh "$containerName" "$matchEnd")
-
-if [[ -z "$containerId" ]]; then
- echo "ERROR: Unable to find the Nextcloud container"
- exit 1
-fi
-
-echo "$containerId" > /tmp/containerId
-
-echo "*/$NEXTCLOUD_CRON_MINUTE_INTERVAL * * * * /cron-tasks.sh $containerId" \
+echo "*/$NEXTCLOUD_CRON_MINUTE_INTERVAL * * * * /cron-tasks.sh" \
> /var/spool/cron/crontabs/root
echo "Starting crond"
diff --git a/scripts/find-container.sh b/scripts/find-container.sh
index 19ebd8d..ecce0f9 100755
--- a/scripts/find-container.sh
+++ b/scripts/find-container.sh
@@ -1,9 +1,15 @@
#!/usr/bin/env bash
set -e
-containerName="$1"
-matchEnd="$2"
+if [[ ! -z "$NEXTCLOUD_PROJECT_NAME" ]]; then
+ containerName="${NEXTCLOUD_PROJECT_NAME}_"
+else
+ matchEnd=","
+fi
+containerName="${containerName}${NEXTCLOUD_CONTAINER_NAME}"
+
+# Get the ID of the container so we can exec something in it later
docker ps --format '{{.Names}},{{.ID}}' | \
egrep "^${containerName}${matchEnd}" | \
awk '{split($0,a,","); print a[2]}'
diff --git a/scripts/healthcheck.sh b/scripts/healthcheck.sh
index c58f6d1..d5f877b 100755
--- a/scripts/healthcheck.sh
+++ b/scripts/healthcheck.sh
@@ -5,4 +5,4 @@ set -x
ps -o comm | grep crond || exit 1
# Make sure the target container is still running/available
-docker inspect -f '{{.State.Running}}' "$(cat /tmp/containerId)" | grep true || exit 1
+docker inspect -f '{{.State.Running}}' "$(/find-container.sh)" | grep true || exit 1