>
you're reading...
Amazon Web Services, General

Implementing Cron Jobs with Elastic Beanstalk


Mechanical clock stop watch, illustrationIf you have worked with cron jobs with Elastic Beanstalk then you know they can be confusing starting out. Here is a simple solution that I use. It prevents duplicate cron tasks, and makes adding new cron jobs really easy!

1. In your .ebextensions/project.config, add a container command (it is listed first in my application):

01_clear_crontab:
        command: "scripts/clear_crontab.sh"

2. Add a script (in my case a “scripts” subfolder in my project root, clear_crontab.sh:

#!/bin/bash

# BACKUP EXISTING CRONTAB
crontab -l > /tmp/cronjob

# REMOVE EXISTING CUSTOM JOBS TO AVOID DUPLICATES
cat /tmp/cronjob | grep -v "/celery_worker." > /tmp/cronjob

# NEW CRONJOB RULES
echo "*/2 * * * * /opt/python/current/app/scripts/celery_worker.sh" >> /tmp/cronjob
echo "*/5 * * * * /opt/python/current/app/scripts/job_two.sh" >> /tmp/cronjob
echo "0 4 * * * /opt/python/current/app/scripts/job_three.sh" >> /tmp/cronjob
echo "* * * * * /opt/python/current/app/scripts/health_check.sh" >> /tmp/cronjob

# IMPLEMENT UPDATED CRONJOB RULES
crontab /tmp/cronjob
rm /tmp/cronjob
echo 'Script successfully executed, crontab updated.'

Explanation:

1. Export the current cron jobs to a temporary file.

2. Remove custom cron jobs (that might be added by another process) by filtering with grep.

3. Add new cron job rules using echo with “>>” to append to the crontab rather than overwrite.

4. Register the crontab and remove the temporary file.

Advertisements

About christopherjcoleman

Independent IT Consultant. Cloud Expert. United States Navy Veteran. Dedicated. Focused. Driven. I make companies better by developing applications to meet specific business needs on reliable, cost-efficient cloud infrastructure. If the right solution doesn't exist then create it. I have achieved my greatest accomplishments because someone else told me "it's not possible; there is no way to do it" - and now there is.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: