Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker ubuntu cron -f is not working

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?

like image 406
Boaz Avatar asked May 29 '15 11:05

Boaz


People also ask

Why is CRON not working with Docker's layered file system?

(*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.

Why does my cron job run multiple times in Docker?

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.

How do I set up Cron in Docker?

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.

How do I start a cron job that is not running?

(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).


1 Answers

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....

like image 185
larsks Avatar answered Oct 12 '22 12:10

larsks