Crontab in Amazon Elastic Beanstalk

0 votes

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

    command: "crontab -r || exit 0"
    command: crontab */2 * * * * rake send_email:test
    leader_only: true

I receive this error:

Failed on instance with return code: 1 Output: Error occurred during build: Command 02send_test_email failed .


I tried next:


*/2 * * * * rake send_email:test > /dev/null 2>&1


  command: "cat .ebextensions/crontab.txt | crontab"
  leader_only: true

RESULT: No errors, but it does not work.


crontab -l > /tmp/cronjob
echo "*/2 * * * * /usr/bin/wget http://localhost/crontabs/send_test_email > /dev/null 2>&1" >> /tmp/cronjob
#echo "*/2 * * * * rake send_email:test > /dev/null 2>&1" >> /tmp/cronjob

crontab /tmp/cronjob
rm /tmp/cronjob
echo 'Script successful executed, crontab updated.'


  command: "/bin/bash .ebextensions/"
  leader_only: true

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

Oct 3, 2018 in AWS by eatcodesleeprepeat
• 4,750 points
this one worked for me as well.. thank you very much, it's a life saver
Did it work even with the rake task?

1 answer to this question.

0 votes

Updated for 2018

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


    mode: "000644"
    owner: root
    group: root
    content: |
      56 11 * * * root . /opt/elasticbeanstalk/support/envvars && cd /var/app/current && /opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test >> /var/app/current/log/cron.log 2>&1

    command: "rm -f /etc/cron.d/*.bak"

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:

    $ cd /var/app/current
    $ which bundle
    $ which rake

    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:

    /opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test

    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:

. /opt/elasticbeanstalk/support/envvars && cd /var/app/current && /opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test >> /var/app/current/log/cron.log 2>&1

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.

answered Oct 3, 2018 by Priyaj
• 57,700 points

Related Questions In AWS

0 votes
1 answer

Using reserved instances in an Elastic Beanstalk Load Balancer

A Reserved Instance a method of pre-paying ...READ MORE

answered Aug 2, 2018 in AWS by bug_seeker
• 15,510 points
0 votes
1 answer

What is command to install Visual C++ in Elastic Beanstalk?

Below solution worked for me Download the redistributable ...READ MORE

answered Aug 28, 2018 in AWS by Archana
• 4,150 points
0 votes
1 answer

Configure Application load balancer to route http to https in Elastic beanstalk

From this document: To update your Elastic Beanstalk environment to ...READ MORE

answered Sep 6, 2018 in AWS by Priyaj
• 57,700 points
0 votes
1 answer
+1 vote
2 answers

Using reserved instances in an Elastic Beanstalk Load Balancer

A Reserved Instance a method of pre-paying ...READ MORE

answered Aug 1, 2018 in AWS by Priyaj
• 57,700 points