Experience is the mother of all teachers, as they say. (...Anonymous)
I was assigned a small project to do.
It was about a prediction app that needed to be able to update its fixtures on the go.
🌳 I was advised by a senior back-end developer to use the cron job concept or the worker process.
We want to write a task scheduler in a nutshell. Here's how to use Node Js to run simple task scheduler. 
Steps:
- Install node-cron : npm install node-cron 
- Install shell for windows : npm install shelljs --> This is a Unix shell commands for Node.js 
So let's write a simple function that runs every seconds
const cron = require("node-cron");
let shell = require("shelljs");
cron.schedule("* * * * * *", () => {
  console.log("Scheduled");
  if (shell.exec("dir").code !== 0) {
    console.log("Something went wrong");
  }
});
Send automated emails with Node-Cron
let cron = require('node-cron');
let nodemailer = require('nodemailer');
let shell = require('shelljs');
// e-mail message options
let mailOptions = {
  from: 'enter your mail here',
  to: 'enter receipient's email here',
  subject: 'Email from Node-App: A Test Message!',
  text: 'Some content to send',
};
// e-mail transport configuration
let transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'your user email here',
    pass: 'Your password here',
  },
});
cron.schedule('* * * * *', async () => {
  // Send e-mail
  await transporter.sendMail(mailOptions, function (error, info) {
    if (shell.exec('dir').code !== 0) {
      console.log('Something went wrong');
    }
    if (error) {
      console.log(error);
    } else {
      console.log('Email sent: ' + info.response);
    }
  });
});
Thanks fro reading....
 
 
              
 
    
Top comments (1)
Hi Abayomi,
How can I use this node-cron to automate and execute my node.js script in another dir?
please help me!