Reputation: 1494
I have my app with Elastic Beanstalk and I need to create a cronjob that run a task in rails
rake "sitemap:generate"
and I wonder if I can do it with Amazon SQS, anyone knows how to do that?
I tried to do a crontab but it doesn't work in the beanstalk...
files:
"/tmp/cron_job.sh":
mode: "000777"
content: |
#!/usr/bin/env bash
*/2 * * * * cd /var/app/current/ && RACK_ENV=production bundle exec rake sitemap:generate
encoding: plain
container_commands:
01_delete_cron_jobs:
command: "crontab -r -u ec2-user || exit 0"
02_add_cron_jobs:
command: "crontab /tmp/cron_job.sh -u ec2-user"
leader_only: true
Is there another way to do a cronjob in Elastic Beanstalk?
Thank you.
Upvotes: 0
Views: 4954
Reputation: 79
Looks like an old question, but it pops up in google search. So here is my attempt You can create a periodic job (like cron jobs) using SQS with your rails app.
Create an "worker tier" env which uses SQS for queueing
in the home folder of your app create a file "cron.yaml"
sample content like
#cron.yaml
version: 1
cron:
— name: "schedule"
url: "/schedule"
schedule: "0 */12 * * *"
also checkout: https://medium.com/@joelennon/running-cron-jobs-on-amazon-web-services-aws-elastic-beanstalk-a41d91d1c571
the gem "active-elastic-job"
Upvotes: 0
Reputation: 2808
You can run a rake task in a crontab in Elastic Beanstalk as follows.
You may need access to configuration that is set in your profile. If you put the following in a shell script like generate_map.sh, it will include the config that your app is using (this may be overkill for your application). Put this script file in the root of your application.
#!/bin/bash
source /etc/profile
cd /var/app/current
rake sitemap:generate
Put the cronjob you want to run in a text file (like file_with_cron_commands.txt) in your project in the .ebextensions directory. For example, if you want your task to run every minute, that file would include the line below. Make sure to leave a blank line at the end of your text file.
* * * * * root cd /var/app/current ; sh generate_map.sh
In your .config file, use a container command to copy it to the appropriate cron tab directory and set the permissions on it.
container_commands:
01_run_my_gen_map_cron:
command: "cat .ebextensions/file_with_cron_commands.txt > /etc/cron.d/my_genmap_cron && chmod 644 /etc/cron.d/my_genmap_cron"
Upvotes: 1