Cron works using every time with VM, Docker & Dokku
I have a Ruby on Rails application in a Docker container on Ubuntu 14.04. I've set up my deployments with Dokku, but I'm not sure how my cron works properly.
Currently I use whenever there is a gem that allows me to do something simple:
every 5.minutes do
runner 'MyModel.run_something'
end
The problem is that I think every time I deploy with git push dokku master
it drops the container and sets it back to default, deleting all my cron schedules.
So, I thought that maybe the cron scheduling should be outside the container and at the VM level.
I am not currently seeing any cron jobs, no matter what I do. This is what happens when I run crontab -l
when ssh'd:
root@dashboard:~# crontab -l
no crontab for root
I'm new to container virtualization, so I'm sorry if I missed a critical part of this, but I'm stumped.
source to share
Took me forever to work on this - ended up calling the rails command instead of crontabs. I also have a rails app downloaded to dock from ubuntu on a digital ocean server. Trying to get "When a gem to work" ... it just isn't. whenever -i
does not work.
Whenever no new crontabs are actually created for the dokku environment. This is useful for figuring out the Cron syntax!
So this is how I got the scheduled tasks to work in dock ...
- Manually create your own crontab via
sudo crontab -e
which will open it in vi / vim
You can use sudo crontab -r
to remove it or sudo crontab -l
to view the current crontabs
- Add the following code to your new crontab
The following code will run every 1 minute.
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
* * * * * /bin/bash -c 'dokku run appname rails r MyModel.run_something'
Make your environment variables equivalent to those you have in the command env
-
grep CRON /var/log/syslog
to view the log of results for troubleshooting. You may need to postfix viasudo apt-get install postfix
for Cron to send email notification of errors, otherwise you might get an error"(CRON) info (No MTA installed, discarding output)"
from syslog. -
cat /var/mail/root
to view the mail received from Cron, indicating errors if the cronjob is down.
Hope this is helpful. At least it got to me!
source to share
Whenever it probably doesn't work because the cron daemon isn't running in your Docker / Dokku container. Docker will run only those processes referred to using either the directive CMD
or RUN
, in any script, executable by one of these directives.
The Dokku guys said bluntly that cron is not supported in Dokku , although they didn't say why. A quick search cron
in Dokku , Buildstep and Dokku base repos image yields no results, so Dokku never seems to start the cron service when creating / running the app.
The suggested solutions are either set up a cron job on the host machine (as you already figured out), use a web scheduling service , or try Heroku Scheduler .
source to share