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

i3wm essentials - II

Welcome back! Let's continue this guide with other setup essentials for i3. Enabling Mousetap Chances are that if you're using a laptop, then tapping on the mousepad does not equal a click for you. You need to enable tapping in your config. Fortunately, there is one documentation available that works for majority of the setups. I don't need to explain this one in detail. Here you go: Enable tap to click in i3 . Volume Control This one is simple again. Do you remember the i3 config file I talked about in the previous blog ? All you need to do is go to that file and find the line: bindsym XF86AudioRaiseVolume Just below that line you will find lines with XF86AudioLowerVolume and XF86AudioMute too. Anyway, the truth is, there are 2 sets of lines with these keywords. Chances are that the line: bindsym XF86AudioRaiseVolume exec --no-startup-id pactl -- set-sink-volume 0 +5% Will be uncommented and the line: bindsym XF86AudioRaiseVolume exec --no-startup-id pactl -- set-sink vo

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

i3wm essentials - I (Brightness)

So you have started using i3 and somehow managed to open your browser and almost resumed your normal work.  But wait, the brightness is too much isn't it? Or is it too low? The mousepad used to work fine, but now all of a sudden tapping does not equal click?!  Don't worry.  This blog series will tell you all about the essential setup commands and common shortcuts that I use to navigate my work in i3, and how you can too. Changing the brightness So you just started i3 and you just can't take this brightness setting. You go for your function keys, and damn! They aren't working. Quick fix: Run the following command if you need to change the brightness ASAP. xrandr -q | grep ' connected' | head -n 1 | cut -d ' ' -f1 This will give an ouput that's the name of your monitor.  Use that monitor name here and change the values of brightness to suit your needs. xrandr --output <monitor-name> --brightness 0.7 Now that your eyes are comfortable, let me show