Implementing Cron Jobs with Elastic Beanstalk - First Cloud Consulting

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.