CRON jobs in PM2



CRON Job pm2?

introduction


pm2 cron jobs are the technique through which we can run any program at a given time. cron jobs are used for the execution of some program at a given time so sometimes we need to take data backup instead of taking data backup you can simply write one script and run it in pm2 cron (e.g if you want to take data backup every week you can do it with it.


use cases

1) Take data backup after a certain time period

you can write a script to take data backup 

2) Delete log files after a certain time

if logs occupying unnecessary space of your server then its always better to delete them after a certain time

2) Restart Server

if you want to restart the server for any reason you can simply do it using pm2 cron jobs

4) Monitor server

monitoring server includes checking ram, server space, server condition, speed, etc

What is pm2?

Pm2 is a process manager which means whatever the services we are trying to run we can manage that through pm2.  processes could be a program like javascript e.g server.js.


What is CRON?

 Cron jobs are tasks scheduled to execute at a certain time. e.g clear logs after every 10 days. We can also invoke some testing functions through it. With pm2 CRON we can restart the file at a certain time span


There are several ways to use pm2 CRON lets see that one by one.

Type 1

Using the pm2 command line.this is one of the easiest ways to execute pm2 commands to start pm2.
pm2 start server.js --cron "*/1 * * * *" //here 1 representing minutes

Type 2

Using pm2 ecosystem.config.js
let path = require("path");

module.exports = {

  apps: [
      {
      name: "cronjob",
      script: "./log/cronjob.js",
      watch: true,
      instances: 1,
      exec_mode: "fork",
      error_file: "./err.log",
      out_file: "./out.log",
      cron_restart: "*/1 * * * *", // restart every minute (used for testing)
      autorestart: true 
    }
  ]
};

Type 3

Using pm2-API 

      using pm2 API we can use the same functionality as the command line but things remain under control with API we can add javascript code in between we can take care of exceptions we can log our output using our favorite log library we don't need to check pm2 logs (although you can also change pm2 log location) but still, it doesn't allow us what to log if you want to log something extra you can use API and write you log code in between.
   var pm2 = require("pm2");
pm2.connect(function(err) {
  if (err) {
    console.error(err);
    process.exit(2);
  }
  pm2.start(

    {
      name: "pm2ManageLogDeletion",
      script: "./log/pm2Logs.js",
      watch: true,
      instances: 1,
      exec_mode: "fork",
      cron_restart: "*/1 * * * *", // restart every minute (used for testing)
      autorestart: true // this is the problem here
    },
    
    function(err, apps) {
      pm2.disconnect(); // Disconnects from PM2
      if (err) throw err;
      console.log("pm2 started ");
    }
  );
});
 

Post a Comment

0 Comments