Skip to main content

Quick Guide to CRON syntax

Cron jobs are used to schedule recurring or one-time jobs by a specific time. The libraris allowing these jobs use something known as cron patterns. This is a blog post to quickly and simply explain their usage and functionality.

Tip: Find out the finest granularity of the library function you are using.

The cron job you are using will specify the finest granularity. This can be explained as the least count of the library function.
For example, if it says, 1 minute, the smallest cron job you could run is at the specification of a minute.
So a job that has to be run by specifying the second-th time is not possible.
If the finest granularity is 1 second, you can even run a job at say, at 15 seconds past 4:35 pm.

The best cron library to use is: cron.

Cron Syntax

Assuming the finest granularity is 1 second, a cron pattern has fields.
These fields are:
field          allowed values
-----          --------------
second 0-59
minute 0-59 hour 0-23 day of month 0-31 month 0-12 (or names, see below) day of week 0-7 (0 or 7 is Sun, or use names)

They are defined in the below cron function as (*)s:

var job = new CronJob("* * * * * *",
() => console.log('This message displays every second')

The (*)s are the fields.
Consider these fields as returning boolean values.
Let's say, each field checks the value entered in the function (* in this case) with the current time.

A (*) indicates true for any value.

*    *   *   *   *   *
sec min hr date mon day

T T   T   T    T   T
(T = True)
The cron job checks these all of these fields with the current time of the computer. Only when all of these fields return true, is the function executed.
So this is read as, true for any second, true for any minute, true for any hour, true for any date, and so on.

Running once

Now that you know the syntax, what can you do to make this job run just once?
Let's say I want to run this function only at 06:00 am.

var job = new CronJob("00 00 06 * * *", () =>
console.log("This message displays at 06:00 am")

So when the time for hour matches 6, when the time for minute matches 0, and when the time for second matches 0, the function will be executed.

Why the 00 in the first field?
If you remove the 00 from the first field, the function would fire 60 times. That is, for every second at 06:00 am, it would check the syntax, and it will find the cron value matches the current time value. Remember, (*) means true for any value.
So we need it to fire only once, and 00 shall make the function execute at the very first instance the clock strikes 6 am.

Any bugs in the previous code?
Yup, as you can see, the remaining values are set as *. Which means that no matter what date, month or day of the week it is, the function will run.
This will not run just once, it will run everyday. You need to set the exact date, month and day if you need it to run exactly once.


Popular posts from this blog

Namaste JavaScript Quick Notes

Note:  Akshay Saini's Namaste JavaScript is probably the best course for JavaScript developers out there. These are my personal notes that I made while watching the course; they serve more of as an online quick reference for my understanding and revision, and I hope it benefits anyone reading it too! Everything in JS happens inside an Execution Context. Before a JS code is run, memory is allocated and variables are set as undefined   , and functions are set as their exact code in the scope within the Execution Context. The global execution context hosts all the global variables and function definitions. An Execution Context has 2 components: Memory, that stores variables and functions; and Code, that reads and executes the code. Call Stack maintains the order of execution contexts. Since JS is single threaded and asynchronous, at one point of time, only one function is executed which is at the top of the call stack. For each function, an execution context is created before executi

An introduction to APIs

API is an acronym for Application Programming Interface. Let's start with first defining some basic terms: Browser: These are browsers. To visit any website on the internet, you need a browser. Server: Hmm, this is tough. In simple words, server is a computer. Yes, just like the laptop, or PC at your home. The only difference is that it does not have a screen. Of course, there are other differences in technical specifications, but at its core, the server is just, simply, a computer. That's it. So why is it called a server? Because it serves . When you go to a website like , your computer connects to the internet and gets you your search result. But your computer's internet connection has to get that result from somewhere, right? If the google search result is giving you some answers, the answers have to come from somewhere. What is that place? The answer to that some place is: a server. When you click on the search button on google, or hit enter after typing, &q

Review: Nestjs - Finally a scalable way to build APIs

I have been thinking about this for a long time. There HAS to be a defined way to build APIs in a scalable way.  If you have used Node, Express, etc in your side projects, you might have felt that after a point in development, debugging truly becomes a pain. Sure, enterprise-level API codes are great. But a lot of times, these configurations are too much, and probably not even needed in other projects. To be honest, I haven't seen a lot of Open-Source API codes either to make a judgement on how experienced developers build their APIs. Anyway, I came across an amazing framework recently, and I think if you are coding a complex API, this should be your way to go. Nest.js Nest.js is a framework for building efficient, reliable and scalable server-side applications.  You essentially break your APIs into controllers, services, and modules, which allow you to modularize the smallest of functionalities in your endpoints or the API as a whole. Why is modularizing important? As I have talk