Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a custom npm script with PM2

I am currently developing several Telegram bots but I want to keep all of them in the same git repository. The issue is that on the other hand, I want to run them as separate processes.

Since I'm using the Telegraf framework, to run a bot it goes such as: micro-bot src/bot-one/bot.js

The problem comes when doing this with PM2. I've been able to run one of the bots with the npm start script like this:

pm2 start --name "WeatherBot" npm -- start -- -t <
TOKEN>

But I'd like to be able to create custom scripts like this:

"main": "src/weatherWarnBot/bot.js",
"scripts": {
    "start": "micro-bot",
    "littleAppleBot": "micro-bot src/littleAppleBot/bot.js",
    "weatherWarnBot": "micro-bot src/weatherWarnBot/bot.js"
}

But, how would the PM2 command be to run each of the two custom scripts? I was thinking of having the bot tokens set as enviroment variables of the system, for simplification.

like image 865
Javier García Manzano Avatar asked Sep 19 '17 18:09

Javier García Manzano


People also ask

How do I run the NPM start script with PM2?

To run the npm start script with Pm2, you can use the following command (make sure you call the command from inside your project folder): $ pm2 start npm -- start.

What is the difference between PM2 start and run Dev?

pm2 start npm -- run dev. pm2 start starts the process. npm here means you want to start the npm command. -- run dev means run dev is piped to npm. So basically you are running, pm2 start (npm run dev) If you want to add process name add it BEFORE npm: pm2 start --name="MyPRocess" npm -- run dev. Share.

How do I run NPM from the frontend?

First you need to go to that frontend folder where (or wherever) package.json is located then, pm2 start starts the process. npm here means you want to start the npm command. -- run dev means run dev is piped to npm. So basically you are running, pm2 start (npm run dev)

What is pm2 in Node JS?

PM2 is an advanced process manager for running Node.js applications. That provides an easier option to automate a Node.js application. We can quickly do the start, stop, or other operations on applications. Many of the Node.js applications use “npm start” to run.


2 Answers

Use this :-

pm2 start --name "Script Name" npm -- run <YOUR CUSTOM SCRIPT> --

like image 174
Gaurav Avatar answered Sep 25 '22 01:09

Gaurav


Try this:

pm2 start npm -- run littleAppleBot --

pm2 start npm -- run weatherWarnBot --

like image 20
zhangjinzhou Avatar answered Sep 24 '22 01:09

zhangjinzhou