Hi Guest

MobileUser

1 HomePage 1 Jobs 1 WalkIn 1Articles  

Cat Exam

 :) Latest Topic
AJAX
VB.NET
SQL Query
UDDI
CLASS
JavaScript Questions
SharePoint Interview
SilverLight
WCF
general knowledge
 ASP.NET
 :) Hot Jobs
 
 :) Latest Articles


  

 


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



 
 Input Your Comments Or suggestions(If Any)
 Name
 Email(optional)
 Comments


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?






 
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 |