Have you ever wondered how news-letters get delivered to you every month? If yes and you thought that there was a person sitting behind the scenes who manually mail newsletters to each subscriber every month. Then you are wrong!
So any repetitive task or task that requires scheduling can be done using a cron-job!
So what is a Cron-Job?
A cron is basically a background process that executes non-interactive jobs at specific time intervals. Use cases of Cron-Job :
- Scheduling emails for monthly newsletters.
- Logging
- Backups etc
Great! Now that we have a basic understanding of what cron-jobs are and what are its use cases, let's now get our hands dirty by creating a cron-job ourselves that emails users every 10 mins.
Prerequisites
- A local development environment for Node.js.
- node-cron package(for creating cron-jobs).
- nodemailer package(for sending emails).
Step 1: Creating a Node.js Application and Installing Dependencies
Firstly start by opening a terminal and creating a new folder for your project:
mkdir node-email-cron-example
Next, change the directory to the new project directory:
cd node-email-cron-example
Then initialize the project by executing the following command:
npm init --yes
Install the dependencies by running the following command:
npm i node-cron nodemailer
Vola! now you have all things installed and set up so now let's jump to the actual coding part.
Step 2: Scheduling a Task
Create a new file email-job.js
nano email-job.js
Next, import the node-cron,node-mailer library.
const cron = require('node-cron');
const email = require('nodemailer');
Then, add the following code to email-job.js
...
// Schedule task
var transport = email.createTransport({
service: 'gmail',
auth: {
user: '', //put your mail here
pass: '' //password here
}
});
const mailOptions = {
from: '', // sender email
to: '', // reciever email
Subject: 'Cron Job Tutorial', // subject
message: 'An Email from Cron-Job Tutorial by Mohd Jasir Noor Khan' // email body
};
cron.schedule('*/10 * * * * ',() => {
transport.sendMail(mailOptions,(err,info) => {
if(err) console.log(err)
else console.log(info)
})
});
Now you would be wondering what does this '*' syntax mean? If yes then please refer to the following diagram for a better understanding.
* * * * * *
| | | | | |
| | | | | day of week
| | | | month
| | | day of month
| | hour
| minute
second ( optional )
Now if you wanted to create a task that would run every ten minutes it would look something like this
*/10 * * * *
You can refer to this link to get a better understanding of the syntax and play with it
Step 3: Running the Script
node email-job.js
Step 4: Output
Congratulations! you just created a cron-job that would email every 10 minutes, wait don't stop here go forward and create some more complex cron-job. You can refer to some ideas that I have listed below that would help you get better at this.
Ideas:
Create a todo-list with a cron-job that would push reminders to users regarding pending tasks.
Create a job-notifier that would scrape the job portal and if a new job is posted then will notify all the users.
For any Feedback or doubts, please feel free to comment below, and also don't forget to connect with me on Linkedin and Github