diff --git a/Dockerfile b/Dockerfile index 5e983bf..4fe1f0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,13 @@ FROM debian:stable-slim ENV DEBIAN_FRONTEND=noninteractive +# Default is to run cron job in the 10th minute of each hour +# You can override at runtime with -e CRON_MINUTE=xx +ENV CRON_MINUTE=10 +# Default port for http server inside container. +# You can override at runtime with -e PORT=xxxx +ENV PORT=8080 + # Install required packages: curl, cron, busybox (httpd), ca-certificates RUN apt-get update \ && apt-get install -y --no-install-recommends curl cron busybox ca-certificates gnupg dirmngr \ @@ -25,14 +32,8 @@ COPY start.sh /usr/local/bin/start.sh # Ensure scripts are executable RUN chmod +x /usr/local/bin/run_speed_test.sh /usr/local/bin/start.sh -# Add cron job to run every hour at minute 0 -RUN printf "0 * * * * root /usr/local/bin/run_speed_test.sh >> /var/log/cron.log 2>&1\n" > /etc/cron.d/speedtest-cron \ - && chmod 0644 /etc/cron.d/speedtest-cron - # Expose default HTTP port and declare data volume VOLUME ["/var/www/data/speed"] -# Default port for http server inside container. You can override at runtime with -e PORT=xxxx -ENV PORT=8080 EXPOSE 8080 # Start script will launch cron and the HTTP server diff --git a/start.sh b/start.sh index 1aff8a8..b6fb8b0 100644 --- a/start.sh +++ b/start.sh @@ -2,6 +2,9 @@ set -e +CRON_MINUTE=${CRON_MINUTE:-10} +PORT=${PORT:-8080} + # Ensure data directory exists mkdir -p /var/www/data/speed @@ -17,20 +20,17 @@ chmod -R a+rwX /var/www/data || true # We run it in background so server starts promptly. The cron will run hourly. /usr/local/bin/run_speed_test.sh || true & -# Ensure the cron job exists in root's crontab (so `crontab -l` shows it) -CRON_ENTRY="* * * * * /usr/local/bin/run_speed_test.sh >> /var/log/cron.log 2>&1" -if crontab -l 2>/dev/null | grep -F "$CRON_ENTRY" >/dev/null 2>&1; then - echo "cron entry already present" +# Ensure the cron job exists in Cron +CRON_FILE=/etc/cron.d/speedtest-cron +if grep run_speed_test ${CRON_FILE}; then + echo "cron entry already present" else - (crontab -l 2>/dev/null; echo "$CRON_ENTRY") | crontab - - echo "installed cron entry into root crontab" + echo ${CRON_MINUTE} '* * * * /usr/local/bin/run_speed_test.sh >> /var/log/cron.log 2>&1' > ${CRON_FILE} fi # Start Debian cron in background service cron start || cron || true -PORT=${PORT:-8080} - # Start busybox httpd serving /var/www on configured port in foreground if command -v busybox >/dev/null 2>&1; then echo "starting busybox httpd on port ${PORT} serving /var/www"