Trying to run a cron job in a docker container.
HAve a supervisord properly configured
(I see cron -f
in the ps -ef
and if I kill it it respawns)
crontab file (for testing):* * * * * echo hi >> /root/test
I tried putting it in /etc/cron.d/crontab
/etc/crontab
and in /var/spool/cron/crontabs/crontab
Nothing is working - I'm not getting anything in /root/test
Any ideas?
(*system*) NUMBER OF HARD LINKS > 1 (/etc/crontab). A bit of searching told me that cron has a security policy to not work if there are lots of hard-links to its files. Unfortunately Docker's layered file-system makes files have lots of hard-links. To fix it, I added touch /etc/crontab /etc/cron.*/*, to the start up script, before running cron.
Each container would run its own cron daemon, causing scheduled tasks to run multiple times. This can be mitigated by using lock files bound into a shared Docker volume. Nonetheless, it’s more maintainable to address the root problem and introduce a dedicated container for the cron daemon.
You’ll need to ensure cron is installed on each host you deploy to. While it can be useful in development, you should look to integrate cron into your Dockerised services when possible. Most popular Docker base images do not include the cron daemon by default. You can install it within your Dockerfile and then register your application’s crontab.
(Don't ask) see cron job not running from cron.daily Get the hardlink count of cron s config files down to one: do touch /etc/crontab /etc/cron.*/* — (if in docker). I put it in the start-up script. Start cron: service cron start — (If on a basic OS, with no init. As in a lot of base images for use in docker).
You may want to check your crontab syntax; crontab files in places like /etc/crontab
require an extra username field, for example:
* * * * * root echo hi >> /root/test
This is documented (not very prominently) in crontab(5)
:
Jobs in /etc/cron.d/
The jobs in cron.d and /etc/crontab are system jobs, which are used usually for more than one user, thus, additionally the username is needed....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With