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

I want to 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.

Short descriptionYou 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 run when the application is deployed to Amazon EC2 instances.Note: The steps in the following resolution 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, consider using the cron-leaderonly-linux.config file for web environments. If you're using a dedicated worker environment, consider using periodic tasks.

ResolutionCreate or update your configuration fileThere 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 AWS GitHub repository.--or--Create or update an existing configuration file based on the following example: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 0commands:    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 bundleTo 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 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

Related informationConfiguring Elastic Beanstalk environmentsCustomizing software on Linux serversCustomizing software on Windows servers