Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a task job with PM2?

Tags:

pm2

I want to make a repeatable job to send mail every 15 minutes taking data from a database table. In node js I can create the job but through PM2 I don't understand where to place the code and how it works.

like image 491
Milan Mahata Avatar asked Feb 28 '17 05:02

Milan Mahata


People also ask

What is pm2 and how do you use it?

PM2 is an advanced process manager for NodeJS applications that allows you quickly start, control, or stop your node processes. It runs as a daemon on the server and will make sure your app is available 24/7/365! Of course, we at HostArmada want to offer our clients the best tools to manage their NodeJS applications.

How to start a process under PM2?

First thing we need to do is to install PM2 globally on your machine: Let’s get into the basics of how to use it. To start a process under PM2, all you have to do is run pm2 start <app>. App being the name of the file you’re running.

How do I use pm2 in Linux?

Let’s get into the basics of how to use it. To start a process under PM2, all you have to do is run pm2 start <app>. App being the name of the file you’re running. PM2 will output something like this: Pay attention to what it says under id. If you forget just run pm2 list.

How do I start an app in PM2?

Start an app. The simplest way to start, daemonize and monitor your application is by using this command line: $ pm2 start app.js. Or start any other application easily: $ pm2 start bashscript.sh $ pm2 start python-app.py --watch $ pm2 start binary-file -- --port 1520. Some options you can pass to the CLI:

How to stop an application in PM2?

To stop a specified application: $ pm2 stop api $ pm2 stop [process_id] To stop them all: $ pm2 stop all. Note: this will not delete the application from PM2 application list. See next section to delete an application.


2 Answers

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

like image 191
Robbie Avatar answered Oct 02 '22 11:10

Robbie


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); } 
like image 21
Sanket Berde Avatar answered Oct 02 '22 11:10

Sanket Berde