Hi Guest

HomePage

1 1

To improves our performance please Like Share Subscribe(Will boost us)

Different step to schedule CRON jobs with complete code in Node.js?
Question Posted on 01 May 2024

Home >> WEB >> Node.js >> Different step to schedule CRON jobs with complete code in Node.js?

Different step to schedule CRON jobs with complete code in Node.js?
To schedule cron jobs in Node.js you need to use a library which is mainly called "node-cron". And this library allows you to schedule and run cron jobs within your node.js application.

Below is the step by step guidence along with code example which helps you to setup and schedule cron jobs by using library "node-cron".

(1)Initialize a Node.js project:-
Well this is the first step to create a new Node.js project or we can navigate to an existing project.


(2)Install the "node-cron" package:-
Now in this second step we need to install the node-cron package on terminal and run the below command:-

npm install node-cron




(3)Import the "node-cron" module:-
In this third step you need to import the "node-cron" module and below is the syntax to do that:-

const cron = require('node-cron');




(4)Define a cron job:-
Now in this step we will create a function which we want to run on a specified schedule by using cron syntax. Here we will create a simple function which will logs a message:-

function logMessage() {
console.log('Cron job executed at:', new Date().toLocaleString());
}




(5)Schedule the cron job:-
And before to move last step to run the job we will use "cron.schedule" to schedule the function to run according to a cron expression. Here in below code we will schedule the function to run every minute:-

// Schedule the cron job to run every minute
cron.schedule('* * * * *', () => {
logMessage();
});


In above code each asterisk represents a different time unit (minute, hour, day of the month, month, day week). Here, we use asterisks to represent “every” for each unit, so the cron job runs every minute.

(6)Run the Node.js script:-
In this step to start the Node.js we will use "node" command with the .js file name:-

node yourcronjob.js



Below is the full code example to run job every minutes you have:-



cron job  Node.js
Once you run the above code you will get date msg every minutes. And below is the out put to get that data.




cron job output
0
0



Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions
REPL stands for ____________________.
Which of the following is true about EventEmitter.on property?
Which method of fs module is used to truncate a file?
What is Node.js?
Which method of fs module is used to close a file?






Most Visited Questions
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |
     
 
Privacy policy | Terms and Conditions | Blog


If the copyright of any questions or content or syntax belong to you email us we will remove that(info@crackyourinterview.com or crackyourinterview2018@gmail.com)
 

1
Home About Us GroupChat
 
Sitemap Feedback Contact us