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

container_commands:
  01remove_old_cron_jobs:
    command: "crontab -r || exit 0"
  02send_test_email:
    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 .

UPDATE 1

I tried next:

crontab.txt

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

default.config

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

RESULT: No errors, but it does not work.

UPDATE 2

crontab.sh

crontab -l > /tmp/cronjob
#CRONJOB RULES
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.'

default.config

02_crontab:
  command: "/bin/bash .ebextensions/crontab.sh"
  leader_only: true

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

Oct 3, 2018 in AWS by eatcodesleeprepeat
• 4,710 points
5,360 views
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.

+1 vote

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

files:
  "/etc/cron.d/mycron":
    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

commands:
  remove_old_cron:
    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
    /opt/rubies/ruby-2.3.4/bin/bundle
    $ which rake
    /opt/rubies/ruby-2.3.4/bin/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
• 58,020 points

This is my cron-linux.config. It's not working for me

files:
    "/etc/cron.d/cron-log3":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * root . /opt/elasticbeanstalk/support/envvars && cd /var/app/current && node /var/app/current/cron/csv_customer_export_test.js >> /var/log/cron-log3.log 2>&1

commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/cron-log3.bak"
And im using Load Balancer, hence it may not flexible with this.
And i'm trying https://bluefletch.com/cron-jobs-on-load-balanced-multi-instance-elastic-beanstalk/
Hi@Danyrupes,

Your script seems ok. Did you get any errors? Try to run one simple command first. Ans see it's working or not.

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
1,015 views
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,170 points
1,288 views
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
• 58,020 points
2,099 views
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
• 58,020 points
854 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP