How to write cron jobs on AWS EC2 instance

Cron is a time-based job scheduler run on the server in the Unix-like operating system.
To perform such action we write cron expression made of five fields, followed by a shell command to execute.

  1. First, we have to log in to AWS EC2 instance using CLI (Command Line Interface).
  2. For log in, type:
    ssh -i <.pem file> username@ip_address
  3. After successfully log in, type: 
    crontab -e
    then select any editor from the list (preferred nano).
  4.  Type cron command. For example:
    * * * * * php /var/www/html/file/name.php
  5. Now save the changes. If you are using nano editor type Ctrl + x and Select Y and then press Enter.
  6. To list out all the running cron type:
    crontab -l

* is used for every minute, every hour, etc. Here ”php” is shell command followed by file location of php script, separated by white space.

If you want to run nodejs script then type:

* * * * * node /var/www/html/nodejs/file/name.js

Each from five stars represents a job:

How to write CRON jobs on AWS EC2 instance

Allowed special characters are:

Comma (,) – Commas are used to separate the item list.

For example:

  • Cron to run at minute 1 and 10 every hour 
    1,10 * * * * php /var/www/html/file/name.php
  • Cron to run at mid-night and mid-day 
    0 0,12 * * * php /var/www/html/file/name.php

Hyphen (-) – Hyphen defines range.

For example:

  • Cron to run at every minute from 1 minute through 10 minute, every hour
    0-10 * * * * php /var/www/html/file/name.php
  • Cron to run at minute 0 past every hour from 6 AM through 6 PM 
    0 6-18 * * * php /var/www/html/file/name.php

Slash (/) – Slash defines step values.

For example:

  • Cron to run every 5th minute 
    */5 * * * * php /var/www/html/file/name.php
  • Cron to run at minute 0 past every 6th hour 
    0 */6 * * * php /var/www/html/file/name.php

Sleep cron:

For example:

  • Cron to run every minute after sleep of 30 seconds
    * * * * * sleep 30; php /var/www/html/file/name.php

Here are some examples:

  • Once per minute 
    * * * * * php /var/www/html/file/name.php
    or 
    */1 * * * php /var/www/html/file/name.php
  • Cron to run once per hour 
    0 * * * php /var/www/html/file/name.php
  • Cron to run twice per hour 
    0,30 * * * php /var/www/html/file/name.php
  • Cron to run once per day
    0 0 * * * php /var/www/html/file/name.php
  • Cron to run on 1st and 15th of every month
    0 0 1,15 * * php /var/www/html/file/name.php
Categories: Backend

25 Comments

ravi teza · February 26, 2018 at 6:21 PM

0 0 1,15 * * php /var/www/html/nodejs/file/name.php

nominee · April 27, 2018 at 1:25 AM

Тhat is a good tip especially to those fresh to the blogosрheгe.
Simple but very accurate іnfo… Thanks for sharing this one.

A must read post!

Janelle Phifer · March 8, 2019 at 1:44 AM

Thanks , I’ve just been searching for information about
this subject for ages and yours is the best I have discovered till now.

However, what about the bottom line? Are you sure
about the source?

robertacarrington · March 11, 2019 at 12:15 AM

It’s amazing in support of me to have a website, which is beneficial in support of my experience.

thanks admin

gamefly free trial · May 26, 2019 at 5:54 AM

I do agree with all of the concepts you’ve offered in your post.
They’re really convincing and can certainly work. Still, the posts are very brief for newbies.
May just you please extend them a little from next time?
Thanks for the post.

    Vishal Anand · May 27, 2019 at 6:21 PM

    Thank you for your feedback. Next time, I will try to make it more detailed.

user · June 26, 2019 at 4:33 AM

you are in reality a excellent webmaster.
The web site loading velocity is incredible.
It kind of feels thɑt үou are doing any unique trick.
Moreover, The contents are masterpiece. you have performeԁ a ɡгeat job on this
topic!

ro8 · June 26, 2019 at 11:58 PM

It’ѕ hard to come by educated people about this
subϳect, however, you seem like you know what you’re talking about!
Thɑnks

jizz · June 28, 2019 at 7:14 AM

Heⅼlo to all, the contents present at this website are really remarkаblе
for people experience, well, keep up the nice work fellows.

user · July 1, 2019 at 1:55 AM

Ι’ve been exploring for a bit foг any high quality articlеs οr blog posts on this kind of area .
Exploring in Yahoo I ultimаtely stumbled upon this web
site. Studying this information So i am glad to exhibіt that I have a very just right uncanny feeling
I came upon exactly what I needed. I so much for sure will
make sure to do not overlooк this ѕite and provides it a look οn а constant
basis.

you · July 1, 2019 at 9:09 AM

A person neсessarily lend a hand to make critically articles I
migһt state. This is the first time I frequented your website page and thus far?

I amаzed with the analysis you made to create this actual publish amazing.
Great procеss!

    Vishal Anand · July 4, 2019 at 6:00 AM

    Thank you!

18 · July 2, 2019 at 7:18 AM

Helⅼo there! This is my first visit to your blog!
We are a group of volunteers and starting
a new project in a community in the same niche. Your bloɡ provided us beneficial information to work on. You have
done a wonderful ϳob!

    Vishal Anand · July 4, 2019 at 5:58 AM

    Thank you!

user · July 3, 2019 at 4:47 AM

Whү pеople still use to reɑd news papers when in this technological world everything is presented on weЬ?

user · July 7, 2019 at 9:09 AM

I go to see day-tο-day a few blogѕ and blοgs to read articles, excеpt
this blog provides quality based ρosts.

jizz · July 7, 2019 at 5:08 PM

I am actuаlly glad to read this weblog posts which carries tons of helpfuⅼ infoгmation,
thanks for providing these kinds of data.

User · July 10, 2019 at 11:10 AM

After lookіng intо ɑ handful of the blog articles on yoսr site,
I truly appreciate your tecһnique of blogging. I book marked it to
my bookmark site list and will be checking back soon. Take a
ⅼook at my web site too and tell me your opinion.

ro89 · July 23, 2019 at 4:55 PM

Great pⲟst. I was checking constantly this blog and I’m impressed!
Eⲭtremely helpful information specіfically the ⅼast
part 🙂 I care for sսcһ informаtion a lot. I was seeking this pɑrticular information for a long time.
Thank you and good luck.

Renato · July 10, 2021 at 1:25 AM

My partner and I absolutely love your blog and find most of your post’s to be exactly I’m looking for.
can you offer guest writers to write content in your case?
I wouldn’t mind creating a post or elaborating on a lot of
the subjects you write concerning here. Again, awesome web
log!

Stanley · July 22, 2021 at 4:50 AM

Great article! This is the type of information that are meant to be shared across the net.
Shame on Google for now not positioning this submit higher!
Thanks =)

Maritza · August 12, 2021 at 1:15 AM

Touche. Sound arguments. Keep up the amazing work.

Fred · August 30, 2021 at 7:29 PM

I think the admin of this website is really
working hard in support of his site, since here every data is
quality based stuff.

Cole · October 24, 2021 at 9:12 PM

I think this is among the most vital info for me. And i’m glad
reading your article.

Tania · November 27, 2021 at 2:41 AM

I’m extremely impressed with your writing skills as
well as with the layout on your weblog. Is this a paid theme or did you
customize it yourself? Either way keep up the nice quality writing,
it’s rare to see a nice blog like this one these days.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *