1

I am trying to run a test cron job in my node.js app (one just for this job) but it is not console.log() like it should be. It should console.log() every 10 seconds but it won't.

This is my only file in my directory/app other than package.json file and node_modules directory.

The code is here:

var express     = require("express"),
    app         = express(),
    bodyParser  = require("body-parser"),
    mongoose    = require("mongoose"),
    cron = require("node-cron");


mongoose.connect("mongodb://localhost/asset-management-v1");
// mongoose.connect("mongodb://localhost/yelp_camp_v10");
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));


app.use(function(req, res, next){
   res.locals.currentUser = req.user;
   next();
});

var task = cron.schedule('10 * * * *', function() {
  console.log('immediately started');
}, false);

task.start();

My end goal is to check a mongodb collection then look at the objectID of each and if the objectID time stamp is older than 10 days to send out an email using nodemailer. If you see anything other than the thing causing the console.log to fail please point it out so I don't have to come back and bother you all more.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Kirbytech
  • 634
  • 1
  • 5
  • 18

1 Answers1

1

10 * * * * - means "every 10 minutes". You need 10 * * * * * to run it every 10 seconds.

And you can pass true to start it immediately:

var task = cron.schedule('10 * * * *', function() {
  console.log('immediately started');
}, true);