I need to stop my nodejs app for some conditions.So I have used process.exit() method. I am running my app with PM2. But PM2 is restarting the app whenever it was stopped. So is there any possibility to disable restarting in PM2.
I think you need the --no-autorestart flag.
"do not automatically restart apps"
http://pm2.keymetrics.io/docs/usage/quick-start/
If you are using pm2 using config json file e.g pm2.config.json or whatever then need to specify similar option "autorestart" to false in your json file.
"autorestart" : false
E.g.
I am using pm2 to run a java application using below json config file
{
"apps" : [
{
"name":"JAVA-Pm2",
"cwd":".",
"script":"/usr/bin/java",
"args":[
"-jar",
"/dir/my-java-app/build/libs/my-java-app-all.jar"
],
"node_args":[],
"exec_interpreter":"",
"exec_mode":"fork",
"autorestart" : false
}
]
}
Simply --no-autorestart works like charm.
Usage example : pm2 start script.js --no-autorestart
If you are using the pm2 config file, use
"autorestart" : false
As suggested by #WitVault BUT make sure you don't have the watch option set to true like so:
"watch": true // This field should NOT be present, otherwise "autorestart" : false is ignored
Related
I have an application built with nextJs and this application should work on a local server (Windows).
my customer told me that he needed this application to work in the background after searching I found that I needed to use a package called pm2 and when I used it gives me an error and I found that I needed to make some configurations for it and I can't found any helping resources, please help 💔
I found that to run nextJs application in the background you will need a custom configuration
you need to download the pm2 globally in your system
create a file with the name ecosystem.config.js in the root folder next to the package.json file
you need to put your config data in this file which would be something like this
module.exports = {
apps: [
{
name: "inventory_test",
script: "node_modules/next/dist/bin/next",
args: "start -p 3333", //running on port 3000
watch: false,
},
],
};
you should set the name as the name you want to see when you check
the list of pm2
the problem will be solved when you set the script as I did in the code above to be more precise the default run of pm2 is to go to the node js folder in the system and try to make start for the application using npm directly but this is the problem we need to make it use the node runner from the nextjs itself or something like this so we change the script as above
after that, we set the arguments that we should run after the npm and in my example is the arg start and choose the port for our application too
and now we make our config
NOTES
you should make build before you start the application
to run the project you will open the folder of the project in the terminal || cmd || cmder and run the command pm2 start ecosystem.config.js
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/
My pm2 process starts using their default ecosystem file structure:
ecosystem.config.js
module.exports = {
apps: [{
env: {
NODE_ENV: "development"
},
error_file: "./logs/error.log",
ignore_watch: ["logs", "node_modules"],
log_date_format: "YYYY-MM-DD HH:mm:ss Z",
name: "my-app",
out_file: "./logs/output.log",
script: "./server.js",
watch: true
}]
}
I start the process with pm2 start ecosystem.config.js and that works fine, with the app reloading on file changes.
But when I stop the process with pm2 stop ecosystem.config.js, and then start it again with pm2 start ecosystem.config.js, pm2 does not watch for files, despite the display column of watching being enabled.
The only way to start the process up again and have the watch work is to delete the pm2 process, and then start up a new one again.
Am I missing something to make a stop or restart work with watch?
Thanks.
The pm2 watch & restart documentation had the answer (must have glossed over it on first read):
Restart with --watch will toggle the watch parameter.
Looks like omitting that --watch flag on already-existing pm2 instances will not toggle the watch parameter in the ecosystem.config.js file. The watch parameter is only toggled on initial process execution, not subsequent ones.
So stopping the process, then starting again with pm2 start ecosystem.config.js --watch does the trick!
Try adding
watch_options: {
"usePolling": true
}
See here: http://pm2.keymetrics.io/docs/usage/watch-and-restart/
This is not a PM2 specific option, but rather a chokidar option which is used by PM2.
The documentation of those options can be found here.
https://stackoverflow.com/users/7575111/nulldev
watch_options: {
"usePolling": true
}
The answer was helpful to me as a trial environment that doesn't cost me restarting the app every time
So I just deployed a site with node and pm2 for the first time and I'm going back and doing some optimization and reading best practices, etc.
I read that you can get a lot of benefit by setting NODE_ENV=production.
I found this in the pm2 docs:
[process.json]
"env_production" : {
"NODE_ENV": "production"
}
...
$ pm2 start process.json --env production
So, I did it but I have no idea if it is working. While trying to figure out how to check it I learned to try:
$ node
> process.env.NODE_ENV
> undefined
So, that's not a good sign.. but, with my limited understanding of how the low level stuff works, I can guess that maybe pm2 launches each app as a separate node process? So maybe I'm not in the right process when I try to check it.
Also, I don't know if I have to make a new ~/.pm2/dump.pm2 file because maybe whenever that is maybe overriding the options I set? (because I used pm2 startup).
How do I check if my pm2 app's NODE_ENV is set?
To answer the actual question in the title:
Within your script, for me my Express app's app.js file, you can use process.env.NODE_ENV to get the current value of NODE_ENV and log that out if you want.
An even better way is to use PM2's Process Metrics module, aka pmx.
yarn add pmx
or
npm install pmx --save
then
const Probe = require('pmx').probe()
Probe.metric({
name : 'NODE_ENV',
value : function() {
return process.env.NODE_ENV
}
})
Now it will show up in calls to pm2 monit (bottom left).
To change your environment:
It is necessary that you kill and restart the process to change your environment.
$ pm2 kill && pm2 start pm2.json --env production
The following isn't good enough:
pm2 restart pm2.json --env production
You can also check your NODE_ENV via running pm2 show <yourServerName>. This will output info about your running server including node env.
In addition, you can check your environment variables via running pm2 env 0. This will show all the environment variables for the running node process.
Start it with npm by adding this to your package.json:
"scripts": {
"myScript": "NODE_ENV=production pm2 start server.js"
}
Then
npm start myScript
You can do it directly too, but this is easy to manage, automate wth crontab and is in your source control...
Your process.json file is incomplete. Try using something like this:
[process.json]
{
"name" : "MyApp",
"script" : "myapp.js",
"env_production" : {
"NODE_ENV": "production"
}
}
Then add logging into your code, preferably somwhere on startup:
console.log("NODE_ENV : ", process.env.NODE_ENV);
Now start the application:
pm2 start process.json --env production
Lastly watch app logs:
pm2 logs MyApp
This should do it.
May be at the start of your server script you can print the value of the environment variable and then check the PM2 logs. Use the following code to print your environment variable value:
console.log('process.env.NODE_ENV:', process.env.NODE_ENV);
And then use the following code to see the PM2 logs
pm2 logs app_name
Here app_name is your process name as indicated by the entry in the process.json file.
You can set Environment variable for pm2 specifically.
go to /etc/systemd/system/ location.
you can see a file named pm2-username.service
file. (eg: pm2-root.service ) you can directly add an Enviorment variable for pm2.
for me, it was LD_LIBRARY_PATH . so I added the line as below after the PATH variable.
Environment=PATH=/usr/local/lib......
Environment=LD_LIBRARY_PATH=/opt/oracle/instantclient_21_1
after that, you can restart or start the node application with update-env flag,
pm2 start yourapp --update-env
try pm2 env <app_name/id> also you can find NODE_ENV in pm2 show <app_name/id>
In your terminal just type:
echo NODE_ENV
it will print current selected environment variable
I am trying to pass some arguments to my Express application which is run by pm2. There wasn't any hint in their documentation to do so, but apparently it's possible to pass some EV to your node application like SOME_STUFF=xxx pm2 start app.js.
Note - after updating environment variables in your environment, you must do the following:
pm2 restart all --update-env
ask me how I know...
Edit: also look for a .env file in the node source directory...
It's actually possible and I'm pretty sure it was in PM2's documentation some time ago.
Anyways, that's what you need to do:
pm2 start app.js -- -some_stuff xxx
Basically, add -- and then you can add your own app parameters.
Managed to find the source, it was hidden quite well: http://pm2.keymetrics.io/docs/usage/quick-start/#42-ways-of-starting-processes
I was having issues passing parameters using pm2 start app.js -- -some_stuff xxx so I opted to do this instead: SOME_STUFF=xxx OTHER_STUFF=abc pm2 start app.js.
Then when I ran pm2 logs I was able to see that my app successfully started and that the environment variables were set correctly where as before I was seeing errors around these variables when I ran pm2 logs.
The environment variables don't always update unless you force them to.
SOME_STUFF=xxx pm2 start app.js --update-env
You should pass ENV in ecosystem.config.js
ecosystem.config.js (in the root)
module.exports = {
apps: [
{
name: "project-name",
exec_mode: "cluster",
instances: "1",
script: "./server/index.js", // your script
args: "start",
env: {
NODE_ENV: "production",
SOME_ENV: "some_value"...
},
},
],
};
In the console:
pm2 start ecosystem.config.js
There is information about configuration of ENV in PM2 official documentation
My node app (sveltekit build) starts in my ubuntu server when I use
node build/index.js
and includes enviroment variables
so with pm2 I found that my app starts with envs starting it:
pm2 "node build/index.js"