Automating cronjob setup with OpsWorks and Chef

In the past trying to automate recurring tasks has been a challenge. I remember trying to Emulate Cronjobs using AWS SWF, and it was a horrible pain, not to mention un-reliable, not because of SWF, but because of my workers crashing (This was the days before I discover supervisor). I even asked the this question on SO: AWS Elastic Beanstalk, running a cronjob.

But now with OpsWorks setting up cronjobs is easier than ever. There is even a helpful documentation entry that is quite helpful!

The gist of it is to create a custom recipe to add each individual cronjob to the crontab.

cron "name_of_cron_entry" do
  minute "0"
  hour "20"
  day "*"
  month "10"
  weekday "1-5"
  action :create

Although this means adding a new recipe every time you need a new cron job, which is annoying. I have taken this concept one step further and have created a custom recipe that uses the custom JSON values to add any Cron job you wish without having to modify or add your recipes. All you need to do is add or modify the values in the custom JSON and re-deploy!

The custom JSON would look like this:

        "custom_env": {
            "staging_site": {    ==> must be the application name
                "cron_jobs": [  
                        // Send an email every sunday at 8:10
                        "name": "send_email_sunday_8",
                        "minute": "10", 
                        "hour":   "8", 
                        "month" :  "*",
                        "weekday": "6",
                        "command": "cd /srv/www/staging_site/current && php .lib/mailing.php" 
                    // Add more cronjobs here.


See the recipe here, and The documentation on how to use the recipe can be found here. Be aware, I'm still testing it and that is why I have it in my develop branch.

Happy deployments!


  1. OpsWorks - Running Cron Jobs
  2. OpsCode Resources: Cron