Crontab in Amazon Elastic Beanstalk

Question:

I am doing a cron tab in AWS – Elastic Beanstalk with Ruby on Rails 3, but I don’t know what is wrong.

I have this code in my .ebextensions/default.config

I receive this error:

UPDATE 1

I tried next:

crontab.txt

default.config

RESULT: No errors, but it does not work.

UPDATE 2

crontab.sh

default.config

RESULT: Works with url, but not with rake task.

Answer:

Updated for 2018

In order to get this to work on the latest version of Elastic Beanstalk, I added the following to my .ebextensions:

.ebextensions/0005_cron.config

How I got there:

There are four main issues to confront when trying to cron a rake task in AWS EB:

  1. The first hurdle is making sure all of your EB and Rails environment variables are loaded. I beat my head against the wall a while on this one, but then I discovered this AWS forum post (login may be required). Running . /opt/elasticbeanstalk/support/envvars loads all of your environment variables.
  2. Then we need to make sure we cd into the current app directory using cd /var/app/current.
  3. Next we need to know where to find the bundle and rake executables. They are not installed in the normal bin directories, but are located in a directory specific to your ruby version. To find out where your executables are located, ssh into your EB server (eb ssh) and then type the following:

    You could probably guess the directory based on your ruby version, but the above commands will let you know for sure. Based on the above, your can build your rake command as:

    NOTE: If you update your ruby version, you will likely need to update your cron config as well. This is a little brittle. I’d recommend making a note in your README on this. Trust me, six months from now, you will forget.
  4. The fourth thing to consider is logging. I’d recommend logging to the same location as your other rails logs. We do this by tacking on >> /var/app/current/log/cron.log 2>&1 to the end of our command string.

Putting all of this together leads to a cron command string of:

Finally, I referenced the latest AWS documentation to build an .ebextensions config file for my cron command. The result was the .ebextensions/0005_cron.config file displayed at the top of this answer.

Leave a Reply