Skip to content

Instantly share code, notes, and snippets.

@hopewise
Last active December 24, 2023 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopewise/31764fdf7a1453872d660b61f64ba723 to your computer and use it in GitHub Desktop.
Save hopewise/31764fdf7a1453872d660b61f64ba723 to your computer and use it in GitHub Desktop.
RubyOnRails cron jobs setup for AWS Elastic BeanStalk
# File: .ebextensions/create_rake_1_caller.config
files:
/tmp/run_cronjob_1.sh:
mode: "000755" # This sets the file as executable
owner: root # Set to the appropriate owner
group: root # Set to the appropriate group
content: |
#!/bin/bash
# Custom script content goes here
echo "Running custom cron job script"
# Other script commands...
export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
cd /var/app/current
/opt/elasticbeanstalk/.rbenv/shims/bundle exec rake cron:test_1 --silent >> /var/log/eb-hooks.log 2>&1
# File: .ebextensions/create_rake_2_caller.config
files:
/tmp/run_cronjob_2.sh:
mode: "000755" # This sets the file as executable
owner: root # Set to the appropriate owner
group: root # Set to the appropriate group
content: |
#!/bin/bash
# Custom script content goes here
echo "Running custom cron job script"
# Other script commands...
export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
cd /var/app/current
/opt/elasticbeanstalk/.rbenv/shims/bundle exec rake cron:test_2 --silent >> /var/log/eb-hooks.log 2>&1
# lib/tasks/cron1.rake
namespace :cron do
desc "Test task"
task test_1: :environment do
# Your task implementation here
puts "Cron job # 1 executed at #{Time.now}"
puts "ENV RAILS_ENV: #{ENV['RAILS_ENV']}"
end
end
# lib/tasks/cron1.rake
namespace :cron do
desc "Test task"
task test_2: :environment do
# Your task implementation here
puts "Cron job # 2 executed at #{Time.now}"
puts "ENV AWS_BUCKET: #{ENV['AWS_BUCKET']}"
end
end
# File: .ebextensions/cronjobs.config
files:
"/etc/cron.d/my_cron_job":
mode: "000644"
owner: root
group: root
content: |
* * * * * root /tmp/run_cronjob_1.sh >> /var/log/eb-hooks.log 2>&1
* * * * * root /tmp/run_cronjob_2.sh >> /var/log/eb-hooks.log 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment