Recently I migrate pypi-mirrors from Vultr VPS to Rancher stack, which is a pure container environment. Everything work fine, though it took me some time to setup the cron job in container correctly. And here is a short summary which might need keep in mind while using cron job inside a container.
1. Environment Variables
Since docker containers variables were set via parameters during the container start. It's not very easy be accessed from the cron job, because the environment variables are always inside the process whose pid is 1 and can not be read outside the process directly.
The work around is exporting the environment variables to a file during the entrypoint run and then source the file before the real cron job command run.
2. Use absolute path as much as possible
For example, prefer to use /usr/local/bin/python
rather than python
as much as possible. In most cases, this will save you time to figure out the $PATH
things.
3. Do necessary logging
Output useful information to stdout/stderr, this will help you when bad thing happens.
4. Properly redirect
In most cases, using something like > /var/log/xxx.log 2>&1
would help you observe if your cron jobs are running as expected.
5. Use .
instead of source
Since /bin/sh
is not always equal to /bin/bash
, using .
to do a source is much safer.
6. Debug
If you really not sure wether your cron job is running, try to install syslog
or rsyslog
, then uncomment the cron
related logging. Then you'll see something in /var/log/cron.log
, which might be useful.