How do I create a cron job that runs a custom script on all Amazon Elastic Compute Cloud (Amazon EC2) instances in an existing AWS Elastic Beanstalk environment?

You can use Elastic Beanstalk configuration files, called .ebextensions, to create cron jobs that run on all Amazon EC2 instances in an Elastic Beanstalk environment. In the Elastic Beanstalk application zip file, create a directory named .ebextensions. The zip file contains configuration files that are run when the application is deployed to Amazon EC2 instances.

The following steps add a cron job that runs on all Amazon EC2 instances in an Elastic Beanstalk environment at the same time.

Note: If you have a periodic task to be run on only one instance, consider using the cron-leaderonly-linux.config file for web environments. If you're using a dedicated worker environment, consider using periodic tasks.

Create or update your configuration file

There are two keys in the cron-linux.config file: files and commands. The files key specifies the location of the myscript.sh file on Elastic Beanstalk instances and the required file permissions. The commands key specifies a list of commands to run on the instances.

Download the cron-linux.config template from the GitHub repository for AWS Documentation (recommended).

Or, you can create or update an existing configuration file based on the example below:

files:
    "/etc/cron.d/mycron":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * root /usr/local/bin/myscript.sh

    "/usr/local/bin/myscript.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/bash

            date > /tmp/date
            # Your actual script content

            exit 0

commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/mycron.bak"

Note: When you name your configuration file, consider that multiple configuration files in the .ebextensions directory are run in alphabetical order by file name. You can name your configuration file cron-linux.config.

The cron-linux.config file creates a cron file named /etc/cron.d/mycron that is configured to run a script every minute. The myscript.sh script outputs the date and then exits when it runs. Every time the cron-linux.config is applied during deployments, a backup of the /etc/cron.d/mycron file is created, named /etc/cron.d/mycron.bak. The last command in cron-linux.config cleans up the /etc/cron.d directory by removing /etc/cron.d/mycron.bak.

For more information about customizing your environment using .ebextensions, see Advanced Environment Customization with Configuration Files (.ebextensions).

Create an application source bundle

To add the configuration file to the application source code of your web application, complete the following steps to create an .ebextensions directory and then add the cron-linux.config file.

1.    In the root of your application folder, create a directory named .ebextensions.

2.    Move the cron-linux.config file that you created or updated to the .ebextensions folder.

3.    Create a zip folder for your application files, including the new configuration file.

The following example shows the structure of the .ebextensions directory and cron-linux.config file in the application zip file:

|-- .ebextensions
|        |-- cron-linux.config 
|        |-- other .config files
|-- other application files

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2016-12-23

Updated: 2019-01-16