How do I create cron jobs on Amazon EC2 instances in Elastic Beanstalk environments?

3 minute read

I want to create a cron job that runs a custom script on all Amazon Elastic Compute Cloud (Amazon EC2) instances. The Amazon EC2 instances are in an existing AWS Elastic Beanstalk environment.

Short description

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 run when the application is deployed to Amazon EC2 instances.

Note: The steps in this example add a cron job that runs on all Amazon EC2 instances in an Elastic Beanstalk environment at the same time. If you have a periodic task to be run on only one instance, then use the cron-leaderonly-linux.config file for web environments. To use the cron-leaderonly-linux-config file, see cron-leaderonly-linux.config on the GitHub website. If you have a dedicated worker environment, then use 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 cron files mycron and on Elastic Beanstalk instances with 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 website. Or, you can create or update an existing configuration file based on the following example:

        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * root /usr/local/bin/
        mode: "000755"
        owner: root
        group: root
        content: |

            date > /tmp/date
            # Your actual script content

            exit 0

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

Note: 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 that's named /etc/cron.d/mycron and is configured to run a script every minute. The script outputs the date, and then exits when it runs. Every time the cron-linux.config applies 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 and removes /etc/cron.d/mycron.bak. For more information, 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:

  1. In the root of your application folder, create a directory that's 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, and include the new configuration file.

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

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

Related information

Configuring Elastic Beanstalk environments

Customizing software on Linux servers

Customizing software on Windows servers

AWS OFFICIALUpdated 13 days ago