I have a pm2 managed app that runs in fork mode. How can I switch it to cluster mode?
You can take a look at this document.
Stop and delete your current app run on the pm2.
pm2 stop ${your app name}
pm2 delete ${your app name}
And rerun your app with arguments -i max:
pm2 start path/to/main.js -i max --name="${your app name}"
// Should use pm2 not pm
Or create a json config file and run your app with it
// processes.json
{
"your-app-name" : [{
"script" : "path/to/main.js",
"instances" : "max",
"exec_mode" : "cluster"
}]
}
pm2 start processes.json
Switch app to Cluster Mode
$ pm2 reload all # Reload all apps in cluster mode
$ pm2 gracefulReload all # Graceful reload all apps in cluster mode
Related
How would I start a pm2 process with the —max-http-header-size node option, as well as name the process.
I have a server with multiple micro-services, one of the services is a web scraper.
This web scraper accepts requests with headers over the nodejs default 8kb limit. So, to run my app locally have to add the --max-http-header-size node option.
I've cloned my app to the server, but don't know how to set --max-http-header-size, nor do I know how to name the process within the pm2 start command.
So far my attempts have looked like this.
// this sets the name, but I don't know how to add the option `--max-http-header-size`
pm2 start npm --name "{REPONAME}" -- start
pm2 start node --name "scraper" --max-http-header-size 15000 app.js
pm2 start node --max-http-header-size 15000 app.js -- --name "scraper"
The accepted answer by David Harvey did not work for me as of Node 16. Instead, I had to use node_args, something like this:
{
"apps" : [{
"name" : "myapp",
"script" : "./app.js",
"node_args": "--max-http-header-size=256000",
"env": {
"NODE_ENV": "production",
}
}]
}
What you're looking for is called environment variables! You can pass environment variables to pm2 using a file that loads your server like this:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
watch: true,
node_args: "--max-http-header-size=16000"
}
]
}
Here's more about it too:
https://pm2.keymetrics.io/docs/usage/environment/
I have tried to set up a cluster by PM2 on Next JS app on Azure App Service (Linux) using startup command "pm2-runtime start ecosystem.config.js" or "pm2 --no-daemon start ecosystem.config.js". Both commands fail. When I am using pm2-runtime the current working directory passed to NodeJS script becomes: "wwwroot/ecosystem.config.js/.next" as a "directory parameters" and the server fails. In second command pm2 --no-daemon I receive information Unknown or unexpected option: --no-daemon. Both commands work for "fork" mode. Is it possible to set up a cluster mode for the azure app service with nextjs app?
ecosystem.config.code
module.exports = {
apps: [
{
name: 'next',
script: './node_modules/next/dist/bin/next',
args: 'start -p ' + (process.env.PORT || 3000),
instances: 2,
exec_mode: 'cluster',
watch: false,
autorestart: true,
},
],
};*
Errors for --no-daemon
enter image description here
Error for pm2-runtime
Error: Could not find a production build in the '/home/site/wwwroot/ecosystem.config.js/.next' directory. Try building your app with 'next build' before starting the production se
rver. https://err.sh/vercel/next.js/production-start-no-build-id
0|next| at Server.readBuildId (/home/site/wwwroot/node_modules/next/next-server/server/next-server.ts:2044:15)
0|next| | at new Server (/home/site/wwwroot/node_modules/next/next-server/server/next-server.ts:199:25)
0|next| | at createServer (/home/site/wwwroot/node_modules/next/server/next.ts:41:10)
0|next| | at start (/home/site/wwwroot/node_modules/next/server/lib/start-server.ts:9:15)
0|next| | at exec (/home/site/wwwroot/node_modules/next/cli/next-start.ts:53:3)
0|next| | at /home/site/wwwroot/node_modules/next/bin/next.ts:113:19 code here
Adding command pm2 list before pm2-runtime solves the issue, I suppose that the command pm2 list initializes the pm2 and sets the proper path for pm2-runtime.
Previously I have had success implementing PM2, but not currently.
My node app does run just fine if I start it manually, but nothing I do gets it to run via PM2, even though PM2 appears to be starting it up. Here's what I mean:
If I run pm2 start server/index.js, the response in the terminal reads:
$ pm2 start server/index.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting D:\Program Files\nodeApps\service-management-api\server\index.js in fork_mode (1 instance)
[PM2] Done.*
Then the terminal prints out a table with App info. It doesn't look pretty pasted here so I'll list it out:
App Name: index
id: 0
version: 1.0.0
mode: fork
pid: 8984
status: online
restart: 0
update 0s
cpu: 0%
mem: 26.0 MB
user: ME
watching: disabled
It appears that the node process should be running. But if I immediately enter pm2 list it shows no processes running. If I enter pm2 stop index, it says:
$ pm2 stop index
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][ERROR] Process index not found
Alternatively, if I try using ecosystem.config.js in the commands, I get similar results. Here are the commands tried:
pm2 reload ecosystem.config.js
pm2 start ecosystem.config.js
Example result of running those commmands:
$ pm2 start ecosystem.config.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] Applications sm_api not running, starting...
[PM2] App [sm_api] launched (2 instances)
And CLI prints table showing two instances with status "online" and watching "enabled". And yet, app isn't running (when tested from browser) and "pm2 show " returns:
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] <app name> doesn't exist
Any clues what's gone awry with my pm2?
Heres my ecosystem.config.js file:
module.exports = {
apps : [{
name: 'sm_api',
script: 'server/index.js',
"log_date_format" : "YYYY-MM-DD HH:mm Z",
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 'max',
error_file : "C:\\pm2_system\\.pm2\\logs\\sm-api-error",
out_file: "C:\\pm2_system\\.pm2\\logs\\sm-api-out",
autorestart: true,
watch: "../",
max_restarts: 10,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
},
exec_mode: 'cluster'
}],
};
Running in Windows Server 2012 environment. Note that I intend to add pm2-windows-service package after I get pm2 working.
Just a note for those who are saying to run this in no-daemon mode, (pm2 start --no-daemon), you should use this mode if you're trying to diagnose the reason why when you run with the daemon, your script fails.
Almost invariably, this is because you're telling PM2 to watch a dist folder or similar while it's building, so PM2 keeps restarting until it hits its limit (because files are being regenerated at pretty rapid speed) and then outputs the "errored" status.
Running in no-daemon is absolutely not recommended in production. Ensure that PM2 is running as a daemon so it can itself restart, and so it can restart your processes as a process itself. If you kill your terminal sessions or they are automatically timed out on your host, you will quickly find your service dies when that happens.
So, in short.. do it the right way and figure out what the problem is, rather than being lazy and turning PM2 into a glorified wrapper for the node binary.
Ok, I got the answer after posting an issue to the pm2 github issues page.
Sharing it here in case anyone else finds themselves in this situation:
https://github.com/Unitech/pm2/issues/4113
(Basically pm2 3.2.5 introduced a bug that causes this issue in Windows. My dev install was 3.2.4. The issue was resolved by reverting to 3.2.4 in production. Simple process, see instructions at link above.)
As a workaround I used the following:
pm2 start --no-daemon app.js
In my case (pm2 v3.2.2):
pm2 stop all //stop all
rm ./pm2/*.pid //delete all
pm2 start app.config.js
I have an electron app which uses pm2 to start some apps using the pm2 module.Everything is fine.However I am trying to implement the following feature:Renaming an app you have started.I know that using the cli I can do the following:
pm2 restart app --name"New name";
So I found the pm2.restart function which takes an Object and a callback as a parameter.So I tried this:
var options = {app:"Blogsport App",name:"New name"};
var callback = function(err){
if(err) {console.log('Failed')}
else {console.log('App renamed')}
};
pm2.restart(options,callback);
This will always log "App renamed".However If I do pm2 list I see that the app has not be renamed.Is there anything I can do to rename an app without deleting it and start it again with a different name?
you can try this:
pm2 restart id --name newName
Example: your id is 1 , then you can type : pm2 restart 1 --name development
you can do
pm2 delete id|name
pm2 start app.js -n newname
or
pm2 restart id|name -n newname
pm2 ls
to see your app id
then you have two options
delete and start again
pm2 delete <id>
pm2 start index.js --name newname
or just restart to keep same id
pm2 restart <id> --name newname
We are trying to start our app with pm2 0.12.8 on ubuntu 14.04 with octa core proccessor. The read me on the git hub has a very straight forward command for running node app in cluster mode.
# Cluster mode
$ pm2 start app.js -i 0 **# Will start maximum processes with LB depending on available CPUs**
$ pm2 start app.js -i max **# Same as above, but deprecated yet.**
But the above command are not working for us. When we try to run these commands only one instance is listed by pm2.
Why?
Any suggestion
Thanks
have you tried starting a fixed number of processes? i.e.
pm2 start app.js -i 2 //should start two instances.
what does "pm2 monit" show you?
also try
pm2 stop all
pm2 delete all
and then
pm2 start app.js -i 0
if you stop a process in pm2 it still reserves one cpu for it even if its not running. you should allways use pm2 delete
Since you are looking to use a process file to manage your pm2, the process file should look similar to this:
// ecosystem.js
{
"apps" : [{
"name" : "API",
"script" : "server.js",// name of the startup file
"instances" : 4, // number of workers you want to run
"exec_mode" : "cluster", // to turn on cluster mode; defaults to 'fork' mode
"env": {
"PORT" : "9090" // the port on which the app should listen
}
// for more options refer : http://pm2.keymetrics.io/docs/usage/application-declaration/#process-file
}]
}
Run this app using the following command to start and stop respectively:
$ pm2 start ecosystem.js
$ pm2 stop ecosystem.js
For fresh process
pm2 start app.js --name "my-node-app" -i 2 // to create 2 process
To make existing running process. You have to stop and delete the current running process, if it was fork mode. Then only it can create cluster mode.
pm2 stop my-node-app
pm2 delete my-node-app
pm2 start app.js --name "my-node-app" -i 2 // to create 2 process
I think you might ever start this project with normal mode (fork_mode), so you should delete all process list before change to cluster mode, since pm2 will memorise ur start options
pm2 delete all
pm2 start app.js -i [NUMBER_OF_INSTANCE|max]
You can get the best information here : pm2 cluster mode
To enable the cluster mode, just pass the -i option:
pm2 start app.js -i max
max means that PM2 will auto detect the number of available CPUs and run as many processes as possible
Or via a js/yaml/json file:
module.exports = {
apps : [{
script : "api.js",
instances : "max",
exec_mode : "cluster"
}]
}
NOTE: you need to set the exec_mode to cluster so PM2 know you want to load balance between each instances, by default it will not
Then to start the Process File:
pm2 start processes.json
The -i or instances option can be:
0/max to spread the app across all CPUs
-1 to spread the app across all CPUs - 1
number to spread the app across number CPUs.