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)
(source: http://crontab.org/)

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

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

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.

Comments

Popular posts from this blog

How Kafka replaced Zookeeper with the (K)Raft algorithm?

Back in 2021, when I first came across Kafka, I remember the DevOps engineer in my team using terms like Zookeeper, broker configs, etc on our team standup calls. I remember not caring about those terms, and simply focusing on learning about the producer, partitions, topics and consumer groups, and how they could be used in the product my team was developing. While platforms like Kafka were built to abstract certain aspects of distributed computing (replication, consistency) while storing & processing logs, it's a pity how so many of us miss out on knowing the amazing engineering that went behind to build the different parts of a platform such as this one. 4 years later, I'm hungry enough to reverse engineer one of my favourite distributed platforms - Kafka! What did the Zookeeper do? To quote the 1st  Kafka paper from 2011, Kafka uses Zookeeper for the following tasks:  (1) detecting the addition and the removal of brokers and consumers,  (2) triggering a rebalance ...

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 ex...

"Hey Google" get me a new T-shirt

Everyone loves Google for its amazing technology and creative workspaces! Guess what? Google loves its developers as much as the world loves it too! And yeah, you don't need to be an amazing programmer to be a developer in Google's community. All you need to do is to spare 30 minutes, just once, and maybe have some creativity! That's it! Oh, and you should be really checking your mails periodically, although if you don't currently have this habit, your excitement would develop that for you. What do we want? So you arrived here to know about getting a T-shirt. Would you also like having a Google Home ? Yup, that is also something you could get through this. And of course, as I mentioned earlier, an entry to Google's Developers Community Program! There're a lot of perks for it but let's first talk business. What do we have to do? In a nutshell: Make an Action for Google Assistant .  But what's an Action ? Action is a feature, or a sub-applicat...