How to write Cron jobs on Amazon Web Services?

Akkireddy
2 min readJun 29, 2020

--

As we know, Cron Jobs is nothing but Scheduled tasks in a software environment that will be running at a specified frequency.

Examples:

  • Sending notifications to users.
  • Sending registered users list to the specified email id..etc.

Here I am going to explain the simple steps to write your own Cron Jobs on AWS EC2 Server.

a. First, you have to Log in to your AWS EC2 instance

b. Run the below command
$ crontab -e

When you run the Crontab command for the first time you will get some of the existed editor's options to open

/bin/ed

/bin/nano

/usr/bin/vim.basic

/usr/bin/vim.tiny

Choose 1–4?

Select option 2 and enter.
Now you will get an editor to add Cron Jobs.

c. Add you're every file path/function path that you want to schedule.
Note that you should specify a single file path per line

For example:

0 10 * * * php /var/www/public_html/api/src/cronjob/notification.php
or
0 10 * * * /bin/php /var/www/public_html/api/src/cronjob/notification.php

Here I am mentioning about PHP file path which sends notifications to the users on every day 10:00am.

Cron Expression Examples:
* * * * * // Run every minute. ex: for 5 mins => */5 * * * *
0 * * * * // Run on the hour, every hour ex: every 2hrs => * */2 * * *
0 23 * * * // Run at 11 p.m. every day
0 0,12 * * 6 // Run at midnight and midday every Saturday
30 2 */2 * * // Run every second day at 2.30 a.m.

5 stars are explained below:
1st star: Minute (ranges from 0–59)
2nd star: Hour (ranges from 0–23)
3rd star: Day (ranges from 1–31)
4th star: Month(ranges from 1–12)
5th star: Day-of-week (0–7. 0 & 7 is Sun. 1-Mon, 2-Tue…etc)

  • The second term is the PHP command path. I.e phpor /bin/php

The third term is PHP file Path i.e /var/www/public_html/api/src/cronjob/notification.php

The command path will change depends on the programming language.

d. Once you enter your Cron Job Commands you have to save it.
To save type Cntrl+x and Select ‘ Y ‘ and Enter.

e. To check whether your Cron Jobs is saved or not, run the below command.
$crontab -l

Now you can see your Cron Job file which has all your Cron Jobs.

--

--

Akkireddy

#DevOps — #AWS — #Cloud enthusiast.. Views are my own.