How to setup cron on AWS Server?

blog image

Setting up a cron is an essential part in every application, where in one can setup crons to take mysql backup, or to send notifications at specific intervals of time or to achieve any functionality without having any human intervention.

What is a Cron Job?

A cron job is a scheduled task or command that is executed automatically at specific intervals. A cron job is defined using a “cron schedule,” which consists of five time and date fields, specifying when the task should run.
Cron schedule looks something like this:

  • An asterisk (*) indicates any value (e.g., every minute if placed in the “Minute” field).
  • A comma-separated list of values (e.g., 2,5,10) specifies multiple specific values for that field.
  • A dash (-) indicates a range of values (e.g., 1-5 for “Day of the week” to run from Monday to Friday).
  • An asterisk followed by a forward slash (e.g., */5) represents steps (e.g., every 5 minutes).

For example, the cron schedule 0 3 * * * would execute a task at 3:00 AM every day, while */15 * * * * would execute a task every 15 minutes.

How to create a cron job?

“crontab -e” command is used to create cron job. This opens the user’s cron table in the default text editor, where you can add or modify cron job entries using the cron schedule format and the command or script you want to run.

How Can I Setup it on AWS?

Taking an example where you want to take mysql data backup and upload it on s3 bucket where the cron runs every day at 8pm, we can follow below steps.

Step 1: Install AWS CLI
sudo apt-get update
sudo apt-get install awscli


aws –version

to see the version of the awscli.

Step 2: Configure the credentials

Run a command,

aws configure

which will show below list

AWS Access Key ID [None]: Enter access key id of IAM user who has permissions to access s3 bucket (20 character string)
AWS Secret Access Key [None]: Enter the secret access key of the user ( 40 character string)
Default region name [None]: Enter the region of your ec2 like us-east-2
Default output format [None]: text or json or table

Step 3: Now, write a script to upload the database backup to s3 bucket in a .sh file

say the file name is

run a command, chmod +x and run this file using ./ to check if your code works properly.

If the code is working, now setup a cron

Simply, open crontab -e and it will open a text editor

0 20 * * * /path/to/

will execute the cron everyday at 8pm and execute the code.

Note: If you are looking for code to upload backup to s3 bucket and send an email with attachment, drop me an email.


Ready to scale up your business?

Follow Us