Skip to content

Instantly share code, notes, and snippets.

@arjanfrans
Last active October 27, 2022 11:22
Show Gist options
  • Save arjanfrans/ff30b2aeaf72a69406b1be17561fb449 to your computer and use it in GitHub Desktop.
Save arjanfrans/ff30b2aeaf72a69406b1be17561fb449 to your computer and use it in GitHub Desktop.
Cronjob debugging in Docker
  1. Does the file end with an empty line? The crontab files must end with an empty line.
  2. Are the file permissions correct? The cron process might not be able to read the files. (Have atleast chmod 644)
  • Example problem:
    • I copied a file into the docker container inside the Dockerfile.
    • I did an sed -i replace inside my entypoint (which causes the file to be newly created).
    • The cronjob was not executed.
    • I fixed it by setting chmod 600 after the sed -i replace.
  1. Inside crontab and /etc/cron.d/ (system crontabs) the cron jobs must have a user specified, for example: * * * * * www-data <job>.
  2. Is cron running in the foreground (cron -f) when running inside a docker container?
  3. When running in docker; use something like Supervisor to start it in the foreground. Otherwise it won't work.
  4. If you're using scripts (inside /etc/cron.daily for example), make sure that the filename is correct: Files must conform to the same naming convention as used by run-parts(8): they must consist solely of upper- and lower-case letters, digits, underscores, and hyphens.. Also make sure the files are executable (chmod +x <file>)!
  5. You can not use environment variable in a cronjob line.
  • Example: * * * * * www-data echo "$SOME_VAR" >> /tmp/crontest
    • A workaround to do something like this using Docker:
      • Copy the file in the Dockerfile
      • Using sed inside the entrypoint to replace the variable. Make sure to set the permissions to 600 (chmod 600) afterwards!
  1. Does the application load environment variables dynamically? The crontab does not run in the same environment as the user! Possible solutions are to source the environment variables (for example using a shell script), or use a .env file for your application.

https://debian-administration.org/article/687/So_your_cronjob_did_not_run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment