How to make a cron job with PM2

后端 未结 6 1083
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 14:25

I want to make a cron job to send mail every 15 minutes taking data from a database table. In node js I can make a cron job but through PM2 I don\'t understand where to plac

相关标签:
6条回答
  • 2020-12-23 14:39

    If you use PM2 ecosystem then in the config file add cron sequence to script param by wrapping it with single quotes. Somehow double quotes didn't work for me.

    module.exports = {
      apps : [{
        name        : "Send-mail",
        script      : "./sendMail.js --cron '*/15 * * * *'",
        watch       : true
      }]
    }
    

    alternatively (my preference)

    module.exports = {
      apps : [{
        name        : "Send-mail",
        script      : "./sendMail.js",
        cron_restart: "*/15 * * * *",
        watch       : true
      }]
    }
    
    0 讨论(0)
  • 2020-12-23 14:40

    You can also use the node-schedule module which allows you to define cron style rules. Then, you can run the program normally in pm2, I use this with PM2 for a lot of projects and it has never let me down.

    var schedule = require('node-schedule');
    
    var rule = new schedule.RecurrenceRule();
    rule.hour = [10]; // 10am
    rule.minute = [0]; // 0mins
    
    var job = schedule.scheduleJob(rule, function(){
        console.log("10am every day")
    });         
    
    //Rule 2 - 6am every wednesday
    var rule2 = new schedule.RecurrenceRule();
    rule2.dayOfWeek = 3; // 0 = Sunday
    rule2.hour = 6;
    rule2.minute = 0;
    
    var job2 = schedule.scheduleJob(rule2, function(){
        console.log("Every Wednesday @ 6am");
    });
    
    
    var rule3 = new schedule.RecurrenceRule();
    rule3.minute = [0, 15, 30, 45]; // Specific Minutes
    
    var job3 = schedule.scheduleJob(rule3, function(){
        console.log("Run at specific minutes")
    });         
    

    It also supports CRON style rules, but I prefer the above method.

    Check out the documentation at https://www.npmjs.com/package/node-schedule

    0 讨论(0)
  • 2020-12-23 14:41

    Use the --cron option:

    -c --cron <cron_pattern>

    For example:

    pm2 start sendMail.js --cron "*/15 * * * *"

    Pm2 will now restart the sendMail.js script on the hour, and at 15, 30 and 45 minutes past the hour

    0 讨论(0)
  • 2020-12-23 14:42

    This is what worked for me, I split the cron in a different file which runs in a different process because i want to free up resources after cron has completed execution.

    ecosystem.config.js:

    module.exports = {
      /**
       * Application configuration section
       * http://pm2.keymetrics.io/docs/usage/application-declaration/
       */
      apps: [
    
        // Main API Hosting
        {
          name: 'API',
          script: 'bin/www',
          env: {
            COMMON_VARIABLE: 'true'
          },
          instances: 1,
          exec_mode: 'cluster',
          watch: false,
          autorestart: true
        },
        {
          name: 'CRON',
          script: "crons/cronjob.js",
          instances: 1,
          exec_mode: 'fork',
          cron_restart: "0,30 * * * *",
          watch: false,
          autorestart: false
        }
      ]
    };
    

    The following lines are important in the cron executable

    cron_restart: "0,30 * * * *" <- cron expression

    autorestart: false <- important because otherwise pm2 will restart the cron after completion immediately

    Also make sure your instances is 1 otherwise multiple cron processes will run.

    Key caveats:

    Whenever you do pm2 restart all, the cron job will run irrespective of the cron expression. If Its critical to run only at specific times, add this additional check in the beginning of the cron file

    if (new Date().getHours() !== 0 ) {
      console.log(`Current hours is ${new Date().getHours()}, not running.`)
      process.exit(0);
    }
    
    0 讨论(0)
  • 2020-12-23 14:48

    thank you for your answer; i do it in this way and just set the email

    1. npm install node-crontab

    2. var crontab = require('node-crontab');

    var jobId = crontab.scheduleJob("*/15 * * * *", function(){

    //This will call this function every 15 minutes
    
    console.log("It's been 15 minutes!");
    

    });

    0 讨论(0)
  • 2020-12-23 14:52

    there are three ways to implement cron through pm2

    1. pm2 command line
    2. pm2 ecosyste.config file
    3. pm2-api

    refer this this helped me .

    0 讨论(0)
提交回复
热议问题