Start multiple processes via npm scripts using PM2 - node.js

I want to start multiple processes by pm2.
I found how to start one process for npm run start:
pm2 start npm -- start
But when I've tried make something like pm2 start npm -- event for npm run event it doesn't start a new process but restarts the first one.
How can I start multiple process by npm and pm2?

Use the --name flag to give each processes a unique name for PM2 to identify them by.
For example:
pm2 start --name=start npm -- start
pm2 start --name=event npm -- run event
Further operations should use the name you have given each process. For example to stop them:
pm2 stop start
pm2 stop event

Related

What are the differences between the processes run by npm command:

Recently I made a simple API server using node.js+express. And the script below is a part of my package.json file I use to run with npm commands.
"scripts": {
...
"release": "cross-env NODE_ENV=production MODE=release node server/app.js",
}
After I starts the server with npm run release, I can see multiple processes such as below are running on my Linux server.
/bin/sh /api/node_modules/.bin/cross-env NODE_ENV=development MODE=test node server/app.js
node /api/node_modules/.bin/../cross-env/bin/cross-env.js NODE_ENV=development MODE=test node server/app.js
node server/app.js
I read a related documentation here, but I don't understand what actually happens in background.
What is the order of creating processes? npm => /bin/sh => node /api/.. => node server/app.js ?
What does each process do? All three processes are necessary to run my server?
If I wants to kill the server with pid, which process id should I use?
What is the order of creating processes? npm => /bin/sh => node /api/.. => node server/app.js ?
What does each process do? All three processes are necessary to run my server?
Well, the flow is like this:
NPM is spawned (you run it) inside your shell, npm itself runs with NPX in order to set the local path.
Your npm script spawns a process from a package called cross-env for cross-OS environment variable setting.
That process in turn spawns Node.js (after setting the environment variables)
That's why you see 3 processes. After your server itself run - only the actual server process is needed to run the server.
If I wants to kill the server with pid, which process id should I use?
This one: node server/app.js - since that's your actual server, the others are just "utility processes" (one for the npm script you ran and the other for the environment variables").
It's worth mentioning that in general - servers are run inside containers or other orchestrators/managers that have built-in logic for restarting/killing the process. Typically the orchestrator sends SIGTERM to the process.

Error when starting pm2 getting module_conf.json not existing

When I start pm2, I get this
Error: ENOENT: no such file or directory, open 'C:\WINDOWS\system32\.pm2\module_conf.json'
how can I fix this?
1 . Add user environment variable as below:
PM2_HOME=%USERPROFILE%\.pm2
2 . Restart command line
cmd
3 . Then kill pm2 daemon once
pm2 kill
4 . Now run pm2 as usual (An example is below)
pm2 start ./bin/www

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.
Try this:
pm2 start npm -- run littleAppleBot --
pm2 start npm -- run weatherWarnBot --
Use this :-
pm2 start --name "Script Name" npm -- run <YOUR CUSTOM SCRIPT> --
pm2 start npm --name "Your APP Name" -- start

How to make a npm stop that terminates the correct forever

I am using forever in npm start to start the node.js app, and I would like to have a npm stop to terminate the task. How can I stop the right task? I really like to not use stop all
Normally you want to assing a uid and then stop the process based on assign name, for example:
1. Starting:
forever start --uid=myapp index.js
2. Stopping only myapp:
forever stop myapp

Forever with npm start and environment variable

I used to start my application using:
DEBUG=chakka ENVIRONMENT=production npm start
How can i start it using forever so i wouldn't have to do it everytime i want to test the application? Thanks!
First you need to know what the application's main script file is. Open up your package.json file and find out what the start script is. If you're using Express it might be app.js. So we'll assume app.js for this example, replace with whatever your file is.
To start the application:
DEBUG=chakka ENVIRONMENT=production forever start app.js
to restart the application after you've made changes:
forever restart app.js

Resources