nextcloud-cronjob/scripts/cron-tasks.sh
Robert Dailey f2ce54653b Fix various custom script processing issues
The following issues were addressed. Note that these are edge cases.

* If an empty `/cron-scripts` directory was mounted in the container, an
  error would occur.
* If files in the `/cron-scripts` directory had spaces in the name, this
  would cause issues.
* stdin was held open when running `docker exec` which would cause hangs
  and other issues. The `exec` command is now run non-interactively.
* Custom scripts that failed would interrupt processing of scripts after
  it, if any.
2020-10-27 16:48:34 -05:00

42 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
set -e
[[ ! -z "$DEBUG" ]] && set -x
source /nextcloud-exec.sh
echo "-------------------------------------------------------------"
echo " Executing Cron Tasks: $(date)"
echo "-------------------------------------------------------------"
# 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
echo "> Nextcloud Container ID: ${containerId}"
run_scripts_in_dir() {
cd "$1"
find . -type f -name '*.sh' -print0 |
while IFS= read -r -d '' file; do
echo "> Running Script: $file"
nextcloud_exec "$containerId" "$(cat $file)" || continue
done
}
# Loop through all shell scripts and execute the contents of those scripts in the Nextcloud
# container.
run_scripts_in_dir /cron-scripts-builtin
# If the user has mounted their own scripts, execute those as well. These are optional. It's done
# this way so that the user may mount more scripts to be executed in addition to the default ones.
if [[ -d /cron-scripts ]]; then
run_scripts_in_dir /cron-scripts
fi
echo "> Done"