How To Setup Cron Job on AWS

Table of Contents
Cron job is a process to run a page automatically in background without interrupting user. You can execute a page automatically at any time on daily basis by setting up a cron job.



       I have already explained about setting up a cron job on godaddy server in one of my article. So now here I am going to explain a step by step process for setting up cron job in AWS too for ubuntu users.

Process to Setting Up AWS Cron:-
Step #1. Login to your AWS instance through Terminal by using your server credentials.
Step #2. Type crontab -e.
Step #3. Now editor will open inside your terminal that will look like below..

How To Setup Cron Job on AWS
Step #4.  You have to add your script command inside it as below.

* * * * * php /var/www/html/myscript.php
Here 5 star shows –
1st * is for minute (0-59)
2nd * is for hour (0-23)
3rd * is for day (1-31)
4th * is for month (1-12)

5th * is for day-of-week (0-7[where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc])


After setting cron time in min, hour, day, month, day-of-week we set our file path which we want to execute automatically according to date & time.


You can edit cron by pressing Insert key from keyboard and to paste your command inside editor you have to press ctrl+shift+v.

Now press esc and after that type :wq to save and exit from editor.
Step #5. Now check your cron is save successfully or not by typing crontab -l in terminal.
Step #6. Make sure you change your file permission executing command chmod 755 /var/www/html/myscript.php in terminal. 
myscript.php

<?php
$to = "anilkumar0805@gmail.com";
$fr_email = 'phpmypassion@gmail.com';
$subj = 'Hi this is from AWS Cron';
/* Create a simple msg body */
$body = "Welcome to myscriptn";
$body .= "n";
// Now send email
mail($to,  $subj, $body, "From: <$fr_email>");
?>

Related posts