Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule Node.js job every five minutes

I'm new to node.js. I need node.js to query a mongodb every five mins, get specific data, then using socket.io, allow subscribed web clients to access this data. I already have the socket.io part set up and of course mongo, I just need to know how to have node.js run every five minutes then post to socket.io.

What's the best solution for this?

Thanks

like image 841
Paul Avatar asked Nov 04 '11 15:11

Paul


People also ask

How do I schedule a job in node JS?

Node Schedule is a flexible cron-like and not-cron-like job scheduler for Node. js. It allows you to schedule jobs (arbitrary functions) for execution at specific dates, with optional recurrence rules. It only uses a single timer at any given time (rather than reevaluating upcoming jobs every second/minute).

What is cron job node JS?

The node-cron module is tiny task scheduler in pure JavaScript for node. js based on GNU crontab. This module allows you to schedule task in node. js using full crontab syntax.


2 Answers

var minutes = 5, the_interval = minutes * 60 * 1000; setInterval(function() {   console.log("I am doing my 5 minutes check");   // do your stuff here }, the_interval); 

Save that code as node_regular_job.js and run it :)

like image 116
alessioalex Avatar answered Sep 17 '22 13:09

alessioalex


You can use this package

var cron = require('node-cron');  cron.schedule('*/5 * * * *', () => {   console.log('running a task 5 minutes'); }); 
like image 20
Hieu Dang Avatar answered Sep 17 '22 13:09

Hieu Dang