From bacb957cb609cb552cccc5a8a9a3d9427b3e2f38 Mon Sep 17 00:00:00 2001 From: Matthias Vogel Date: Mon, 23 Nov 2026 08:11:27 +0000 Subject: [PATCH 1/3] [BUGFIX] Send the output of the cron jobs of docker-cronjob to the container log Cron in Debian and Ubuntu mails the output of a job and discards it where no mail server runs, so the output of the jobs that docker-cronjob adds was lost. docker-cronjob now begins /etc/cron.d/webdevops-docker with SHELL=/usr/local/bin/docker-cronjob-shell, which runs the command with /bin/sh and sends its output to syslog, from where syslog-ng writes it to the container log. With MAILTO set in the crontab file, the script only runs the command and cron mails the output as before; without syslog-ng, the output stays with cron. --- ci/image-tests/Checks/BaseChecks.php | 37 +++++++++++++++++++ .../Checks/ContainerEnvironmentChecks.php | 24 ++++++++++++ .../Hooks/entrypoint.d/70-image-test-cron.sh | 8 ++++ .../conf/bin/usr-bin/docker-cronjob | 6 +++ .../conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../conf/bin/usr-bin/docker-cronjob | 6 +++ .../conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../php/8.2/conf/bin/usr-bin/docker-cronjob | 6 +++ .../8.2/conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../conf/bin/usr-bin/docker-cronjob | 6 +++ .../conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../php/8.3/conf/bin/usr-bin/docker-cronjob | 6 +++ .../8.3/conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../conf/bin/usr-bin/docker-cronjob | 6 +++ .../conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../php/8.4/conf/bin/usr-bin/docker-cronjob | 6 +++ .../8.4/conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../conf/bin/usr-bin/docker-cronjob | 6 +++ .../conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../php/8.5/conf/bin/usr-bin/docker-cronjob | 6 +++ .../8.5/conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../conf/bin/usr-bin/docker-cronjob | 6 +++ .../conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ .../php/8.6/conf/bin/usr-bin/docker-cronjob | 6 +++ .../8.6/conf/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ docs/customization.md | 7 ++++ docs/environment-variables.md | 2 +- docs/images/php.md | 2 +- docs/known-issues.md | 6 +++ .../base/general/bin/usr-bin/docker-cronjob | 6 +++ .../general/bin/usr-bin/docker-cronjob-shell | 34 +++++++++++++++++ 31 files changed, 564 insertions(+), 2 deletions(-) create mode 100644 docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.2/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.3/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.4/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.5/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 docker/php/8.6/conf/bin/usr-bin/docker-cronjob-shell create mode 100644 provisioning/base/general/bin/usr-bin/docker-cronjob-shell diff --git a/ci/image-tests/Checks/BaseChecks.php b/ci/image-tests/Checks/BaseChecks.php index 262c4b25e..50d274748 100644 --- a/ci/image-tests/Checks/BaseChecks.php +++ b/ci/image-tests/Checks/BaseChecks.php @@ -17,6 +17,14 @@ */ trait BaseChecks { + /** The environment of cron for a job, without MAILTO */ + private const string CRONJOB_SHELL_ENV = 'env -i PATH=/usr/bin:/bin'; + + /** A command for docker-cronjob-shell, as cron runs it: it prints CRONJOB_SHELL_OUTPUT and ends with 3 */ + private const string CRONJOB_SHELL_COMMAND = '/usr/local/bin/docker-cronjob-shell -c "echo image-test-cronjob-shell | tr a-z A-Z; exit 3"'; + + private const string CRONJOB_SHELL_OUTPUT = 'IMAGE-TEST-CRONJOB-SHELL'; + /** * @return array */ @@ -138,6 +146,35 @@ public function testBasePackageIsInstalled(string $package): void $this->assertPackageInstalled($package); } + /** + * With MAILTO, which a crontab file sets for its jobs, docker-cronjob-shell only runs the command: its output stays + * with cron, which mails it as before, see docs/customization.md#cron-jobs. The exit code stays that of the command. + */ + public function testDockerCronjobShellLeavesTheOutputToCronWithMailto(): void + { + $result = $this->execute(self::CRONJOB_SHELL_ENV . ' MAILTO=root ' . self::CRONJOB_SHELL_COMMAND); + self::assertSame([3, self::CRONJOB_SHELL_OUTPUT . "\n"], [$result->exitCode, $result->stdout], $result->describe()); + } + + /** + * Without MAILTO, docker-cronjob-shell sends the output of the command to syslog, which syslog-ng writes to the + * container log; in the images without syslog-ng, the output stays with cron. The exit code stays that of the + * command. + */ + public function testDockerCronjobShellSendsTheOutputToSyslog(): void + { + $result = $this->execute(self::CRONJOB_SHELL_ENV . ' ' . self::CRONJOB_SHELL_COMMAND); + if (!in_array('syslog', self::services(), true)) { + self::assertSame([3, self::CRONJOB_SHELL_OUTPUT . "\n"], [$result->exitCode, $result->stdout], $result->describe()); + return; + } + + self::assertSame([3, ''], [$result->exitCode, $result->stdout], $result->describe()); + $this->eventually(function (): void { + self::assertMatchesRegularExpression('/ cron-job\[\d+\]: ' . self::CRONJOB_SHELL_OUTPUT . '$/m', self::container()->logs()); + }); + } + /** * docker-cronjob adds a job in the system crontab format, with its user, see docs/customization.md#cron-jobs, and * cron runs it as that user, also in Alpine, whose BusyBox crond reads no /etc/cron.d. In a container of its own, diff --git a/ci/image-tests/Checks/ContainerEnvironmentChecks.php b/ci/image-tests/Checks/ContainerEnvironmentChecks.php index 04fc34062..1773e10ed 100644 --- a/ci/image-tests/Checks/ContainerEnvironmentChecks.php +++ b/ci/image-tests/Checks/ContainerEnvironmentChecks.php @@ -98,6 +98,30 @@ public function testSyslogMessagesReachTheFileFromTheEnvironment(): void }); } + /** + * The output of a cron job that docker-cronjob added, from Hooks/, reaches the file of LOG_STDOUT in Debian and + * Ubuntu. Cron there would mail it, or discard it without a mail server: docker-cronjob-shell passes both lines to + * syslog-ng, which writes them to the file of LOG_STDOUT. Only in the Debian and Ubuntu images that start cron; + * BusyBox crond on Alpine passes the output on itself. + */ + public function testCronJobOutputReachesTheFileFromTheEnvironment(): void + { + if (!in_array('cron', self::services(), true)) { + self::markTestSkipped('The image does not start cron.'); + } + if (self::image()->isAlpine()) { + self::markTestSkipped('BusyBox crond passes the output of a job on itself.'); + } + + $container = $this->environmentContainer(); + // the job writes the lines in upper case, so they differ from its command, which cron logs + $this->waitFor(function () use ($container): void { + $stdout = $this->stdout('cat ' . static::ENVIRONMENT['LOG_STDOUT'], $container); + self::assertMatchesRegularExpression('/ cron-job\[\d+\]: IMAGE-TEST-CRON-OUTPUT$/m', $stdout); + self::assertMatchesRegularExpression('/ cron-job\[\d+\]: IMAGE-TEST-CRON-ERROR$/m', $stdout); + }, 25, 3); + } + /** * /docker.stdout and /docker.stderr, where the services of the image write their logs, point to the files of * LOG_STDOUT and LOG_STDERR. diff --git a/ci/image-tests/Hooks/entrypoint.d/70-image-test-cron.sh b/ci/image-tests/Hooks/entrypoint.d/70-image-test-cron.sh index 808cafb26..59ff7c6c9 100644 --- a/ci/image-tests/Hooks/entrypoint.d/70-image-test-cron.sh +++ b/ci/image-tests/Hooks/entrypoint.d/70-image-test-cron.sh @@ -1,6 +1,8 @@ # Image tests: with IMAGE_TEST_CRON set to hours, e.g. "9,10", adds a cron job for these hours before cron starts, which # marks that it ran, see ContainerEnvironmentChecks. The tests set the current and the next hour in Berlin, worked out # outside the image, so cron runs the job only if it uses the time zone of TZ. +# It also adds a job with docker-cronjob that writes a line to stdout and one to stderr every minute, in upper case, so +# that the lines differ from the command, which cron logs. if [[ -n "${IMAGE_TEST_CRON:-}" ]]; then if [[ -d /etc/crontabs ]]; then # BusyBox crond on Alpine @@ -9,4 +11,10 @@ if [[ -n "${IMAGE_TEST_CRON:-}" ]]; then echo "* ${IMAGE_TEST_CRON} * * * root echo ran > /tmp/image-tests/cron-job" > /etc/cron.d/image-test chmod 644 /etc/cron.d/image-test fi + + # where docker-cronjob fails, as on Alpine before it wrote to /etc/crontabs, the container starts all the same; the + # test of the output fails then + if id application > /dev/null 2>&1; then + docker-cronjob '* * * * * application echo image-test-cron-output | tr a-z A-Z; echo image-test-cron-error | tr a-z A-Z >&2' || true + fi fi diff --git a/docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob b/docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob +++ b/docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob-shell b/docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/base/ubuntu-22.04/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob b/docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.2-alpine/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.2/conf/bin/usr-bin/docker-cronjob b/docker/php/8.2/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.2/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.2/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.2/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.2/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.2/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob b/docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.3-alpine/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.3/conf/bin/usr-bin/docker-cronjob b/docker/php/8.3/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.3/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.3/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.3/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.3/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.3/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob b/docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.4-alpine/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.4/conf/bin/usr-bin/docker-cronjob b/docker/php/8.4/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.4/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.4/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.4/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.4/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.4/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob b/docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.5-alpine/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.5/conf/bin/usr-bin/docker-cronjob b/docker/php/8.5/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.5/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.5/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.5/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.5/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.5/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob b/docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.6-alpine/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docker/php/8.6/conf/bin/usr-bin/docker-cronjob b/docker/php/8.6/conf/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/docker/php/8.6/conf/bin/usr-bin/docker-cronjob +++ b/docker/php/8.6/conf/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/docker/php/8.6/conf/bin/usr-bin/docker-cronjob-shell b/docker/php/8.6/conf/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/docker/php/8.6/conf/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" diff --git a/docs/customization.md b/docs/customization.md index 34a00dfec..ecf90c3b3 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -159,6 +159,13 @@ Cron in Debian and Ubuntu runs jobs with `PATH=/usr/bin:/bin`, which does not co In `base` and `base-app`, cron does not run by default; also run `docker-service enable cron`, see [Services](#services). +The output of a job, stdout and stderr, goes to the container log: + +- In Alpine, BusyBox `crond` passes it on: stdout to the stdout of the container, stderr to its stderr. +- Cron in Debian and Ubuntu would mail it, and discard it where no mail server is installed. So `docker-cronjob` begins `/etc/cron.d/webdevops-docker` with `SHELL=/usr/local/bin/docker-cronjob-shell`: cron runs each job with this script, which runs the command with `/bin/sh` and sends both outputs to syslog, a message per line and per 1 KiB of a longer line. They reach the stdout of the container, or the file of `LOG_STDOUT`, as `[SYSLOG] cron-job[]: `; the exit code stays that of the command. This needs syslog-ng, which `base` and `base-app` do not start by default (`docker-service enable syslog cron`); without it, the output stays with cron. +- To get the mail of cron instead, as before, add a `MAILTO` line before the jobs; it applies to the jobs below it. In Debian and Ubuntu, for example `RUN docker-cronjob 'MAILTO=ops@example.com' '*/5 * * * * application /usr/local/bin/php /app/bin/console app:cleanup'`. `docker-cronjob-shell` then only runs the command, and cron mails its output. The mail needs a mail server, for example Postfix of the `-dev` images, see [Mail](images/php.md#mail). `MAILTO=""` turns the mail off, and the output goes to syslog. +- For a crontab file of your own, for example in `/etc/cron.d`, put the same `SHELL` line at its top; otherwise cron mails the output, see [Known issues](known-issues.md#output-of-cron-jobs-in-crontab-files-of-your-own). + ### Time zone Cron uses the container's time zone, which is UTC. `PHP_DATE_TIMEZONE` only changes PHP's time zone, not when cron runs a job. To run jobs in local time, set `TZ`. It also changes the output of `date` and the time stamps supervisord writes to the container log. PHP keeps `PHP_DATE_TIMEZONE`, so set both: diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 78a1f8c3e..3ddfc83ed 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -35,7 +35,7 @@ Images: `base`, `base-app`, `php-*`. | Variable | Default | Effect | | --- | --- | --- | | `CLI_SCRIPT` | not set | Command the `cli` entrypoint runs as `APPLICATION_USER`, in `base` as `root`. See [Customization](customization.md#commands). | -| `LOG_STDOUT` | container stdout | File that `/docker.stdout` points to. Nginx, Apache and PHP-FPM write their access logs there, syslog-ng the messages of the services, for example of cron and Postfix. | +| `LOG_STDOUT` | container stdout | File that `/docker.stdout` points to. Nginx, Apache and PHP-FPM write their access logs there, syslog-ng the messages of the services, for example of cron and Postfix, and in Debian and Ubuntu the output of the cron jobs of `docker-cronjob`, see [Cron jobs](customization.md#cron-jobs). | | `LOG_STDERR` | container stderr | File that `/docker.stderr` points to. Nginx and Apache write their error logs there, PHP-FPM its own messages and the errors of the PHP scripts it runs. `php.error_log` gives the PHP errors of PHP-FPM and the PHP CLI a file of their own instead, in a directory that `application` may write to, for example `/app`. | | `TZ` | not set (UTC) | Time zone of the container, for example `Europe/Berlin`. It applies to `date`, cron and the time stamps supervisord writes to the container log. PHP uses `PHP_DATE_TIMEZONE` instead, see [Time zone](customization.md#time-zone). | diff --git a/docs/images/php.md b/docs/images/php.md index 98522f5b4..6b1ea04bb 100644 --- a/docs/images/php.md +++ b/docs/images/php.md @@ -128,7 +128,7 @@ Every PHP version comes as a Debian tag (`8.5`) and an Alpine tag (`8.5-alpine`) | [Build tools for extensions](#installing-more-extensions) | included | installed by `docker-php-ext-install`; for PIE install `$PHPIZE_DEPS` | | [Locales](../customization.md#locales) | several hundred, for example `de_DE.utf8` | none | | [Installing packages](../customization.md#tools-for-your-dockerfile) | `apt-install`, from the Debian components `main`, `contrib` and `non-free` | `apk-install` | -| [`docker-cronjob`](../customization.md#cron-jobs) | adds the lines to `/etc/cron.d/webdevops-docker`; jobs run with `PATH=/usr/bin:/bin` | adds each line without its user to `/etc/crontabs/`; jobs run with the `PATH` of the container | +| [`docker-cronjob`](../customization.md#cron-jobs) | adds the lines to `/etc/cron.d/webdevops-docker`; jobs run with `PATH=/usr/bin:/bin`; their output goes through syslog to the stdout of the container, or with `MAILTO` by mail | adds each line without its user to `/etc/crontabs/`; jobs run with the `PATH` of the container; their output goes to the stdout and stderr of the container | | [`docker-image-cleanup`](../customization.md#tools-for-your-dockerfile) | works | works | | [Apache index files](../environment-variables.md#web-server) in `php-apache` | `index.html`, `index.cgi`, `index.pl`, `index.php`, `index.xhtml`, `index.htm` come before `WEB_DOCUMENT_INDEX` | `index.html` comes before `WEB_DOCUMENT_INDEX` | | [`mail()` without Postfix](#mail) | fails: there is no `sendmail` command | fails: BusyBox `sendmail` finds no mail server | diff --git a/docs/known-issues.md b/docs/known-issues.md index 90d4e8d5c..ddffa8ae6 100644 --- a/docs/known-issues.md +++ b/docs/known-issues.md @@ -22,6 +22,12 @@ These options and tools are part of the images but do not work, or work only in - **Images:** `php-*` with the Debian tag `8.6`. - **Instead:** Write the repository to a file in `/etc/apt/sources.list.d/`, for example `RUN echo "deb http://deb.debian.org/debian trixie-backports main" > /etc/apt/sources.list.d/backports.list`. +### Output of cron jobs in crontab files of your own + +- **Effect:** Cron in Debian and Ubuntu mails the output of a job. Where no mail server runs, it discards it and logs `(CRON) info (No MTA installed, discarding output)`; where Postfix runs, as in the `-dev` images, the output lands in the mailbox of the user, for example `/var/mail/application`. Only the jobs that `docker-cronjob` adds send their output to the container log, see [Cron jobs](customization.md#cron-jobs). +- **Images:** the Debian and Ubuntu images: `php-*` without `-alpine`, `base`, `base-app`. +- **Instead:** Add the jobs with `docker-cronjob`, or put `SHELL=/usr/local/bin/docker-cronjob-shell` at the top of your crontab file in `/etc/cron.d`. + ### Composer 1 on PHP 8.4 to 8.6 - **Effect:** With `COMPOSER_VERSION=1`, every Composer run prints deprecation notices. From PHP 8.5 on, Composer 1 also cannot download packages: commands such as `composer require` stop with `Uncaught TypeError: stream_context_create(): Argument #1 ($options) must be an array with valid callbacks as values`. diff --git a/provisioning/base/general/bin/usr-bin/docker-cronjob b/provisioning/base/general/bin/usr-bin/docker-cronjob index ca4aacb1b..bc5fbca7e 100644 --- a/provisioning/base/general/bin/usr-bin/docker-cronjob +++ b/provisioning/base/general/bin/usr-bin/docker-cronjob @@ -40,6 +40,12 @@ fi touch /etc/cron.d/webdevops-docker chmod 0644 /etc/cron.d/webdevops-docker +# Cron in Debian and Ubuntu mails the output of a job: the jobs of this file run with docker-cronjob-shell instead, which +# sends their output to syslog-ng and with it to the container log, unless a MAILTO line asks for the mail +if [[ ! -s /etc/cron.d/webdevops-docker ]]; then + echo 'SHELL=/usr/local/bin/docker-cronjob-shell' >> /etc/cron.d/webdevops-docker +fi + for CRONJOB_LINES in "$@"; do echo "$CRONJOB_LINES" >> /etc/cron.d/webdevops-docker done diff --git a/provisioning/base/general/bin/usr-bin/docker-cronjob-shell b/provisioning/base/general/bin/usr-bin/docker-cronjob-shell new file mode 100644 index 000000000..ed4b3c0c3 --- /dev/null +++ b/provisioning/base/general/bin/usr-bin/docker-cronjob-shell @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The shell of the cron jobs that docker-cronjob adds in Debian and Ubuntu: docker-cronjob sets SHELL to this script in +# /etc/cron.d/webdevops-docker, so cron runs each job as "docker-cronjob-shell -c ". Cron in Debian and Ubuntu +# mails the output of a job, and discards it where no mail server is installed. This script runs the command with +# /bin/sh, as cron would, and sends its output, stdout and stderr, to syslog, from where syslog-ng writes it to the +# container log. The exit code stays that of the command. +# With MAILTO set in the crontab file, cron mails the output as before: the script then only runs the command. + +# the command runs with the shell that cron would use +export SHELL=/bin/sh + +# MAILTO asks for the mail of cron; an empty MAILTO turns it off, so the output goes to syslog +if [[ -n "${MAILTO:-}" ]]; then + exec /bin/sh "$@" +fi + +# Cron runs @reboot jobs as soon as it starts, at the same time as syslog-ng: wait up to 10 seconds for the socket of +# syslog-ng if supervisord starts it +if [[ ! -S /dev/log ]] && grep -q '^autostart *= *true' /opt/docker/etc/supervisor.d/syslog.conf 2> /dev/null; then + for _ in {1..20}; do + [[ -S /dev/log ]] && break + sleep 0.5 + done +fi + +# without syslog-ng, the output goes to cron as before +if [[ ! -S /dev/log ]]; then + exec /bin/sh "$@" +fi + +# each line, and each KiB of a longer line, as a message of its own, tagged with the process id of the job; should +# logger end early, cat reads the rest, so the command does not end on a broken pipe +/bin/sh "$@" 2>&1 | { logger -t "cron-job[$$]" -p cron.info || cat > /dev/null; } +exit "${PIPESTATUS[0]}" From 286890e76c2bbfe9deafa896d95089ab2955ba2a Mon Sep 17 00:00:00 2001 From: Matthias Vogel Date: Mon, 23 Nov 2026 08:22:33 +0000 Subject: [PATCH 2/3] [TASK] Image tests: one constant for the message of the skipped cron tests --- ci/image-tests/Checks/ContainerEnvironmentChecks.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ci/image-tests/Checks/ContainerEnvironmentChecks.php b/ci/image-tests/Checks/ContainerEnvironmentChecks.php index 1773e10ed..eb3986504 100644 --- a/ci/image-tests/Checks/ContainerEnvironmentChecks.php +++ b/ci/image-tests/Checks/ContainerEnvironmentChecks.php @@ -25,6 +25,8 @@ */ trait ContainerEnvironmentChecks { + private const string NO_CRON = 'The image does not start cron.'; + /** * TZ=Europe/Berlin: date shows the time zone, and supervisord writes local time to the container log. */ @@ -48,7 +50,7 @@ public function testTimeZoneFromTheEnvironment(): void public function testCronRunsJobsInTheTimeZoneFromTheEnvironment(): void { if (!in_array('cron', self::services(), true)) { - self::markTestSkipped('The image does not start cron.'); + self::markTestSkipped(self::NO_CRON); } $container = $this->environmentContainer(); @@ -67,7 +69,7 @@ public function testCronRunsJobsInTheTimeZoneFromTheEnvironment(): void public function testCronLogsAtTheLevelFromTheEnvironment(): void { if (!in_array('cron', self::services(), true)) { - self::markTestSkipped('The image does not start cron.'); + self::markTestSkipped(self::NO_CRON); } $container = $this->environmentContainer(); @@ -107,7 +109,7 @@ public function testSyslogMessagesReachTheFileFromTheEnvironment(): void public function testCronJobOutputReachesTheFileFromTheEnvironment(): void { if (!in_array('cron', self::services(), true)) { - self::markTestSkipped('The image does not start cron.'); + self::markTestSkipped(self::NO_CRON); } if (self::image()->isAlpine()) { self::markTestSkipped('BusyBox crond passes the output of a job on itself.'); From 768241f0ba31833f65592b18b5ea9778cc6b0493 Mon Sep 17 00:00:00 2001 From: Matthias Vogel Date: Mon, 23 Nov 2026 08:24:23 +0000 Subject: [PATCH 3/3] [TASK] Image tests: a blank line after the skip, for Rector --- ci/image-tests/Checks/ContainerEnvironmentChecks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/image-tests/Checks/ContainerEnvironmentChecks.php b/ci/image-tests/Checks/ContainerEnvironmentChecks.php index eb3986504..71a89f928 100644 --- a/ci/image-tests/Checks/ContainerEnvironmentChecks.php +++ b/ci/image-tests/Checks/ContainerEnvironmentChecks.php @@ -111,6 +111,7 @@ public function testCronJobOutputReachesTheFileFromTheEnvironment(): void if (!in_array('cron', self::services(), true)) { self::markTestSkipped(self::NO_CRON); } + if (self::image()->isAlpine()) { self::markTestSkipped('BusyBox crond passes the output of a job on itself.'); }