Running my app with pm2 start index.js --watch functions as expected. I would like to use the config to ignore_watch.
ecosystem.config.js
module.exports = {
apps : [{
name : "appname",
script : "./index.js",
watch : true,
max_mamory_restart : "1G",
ignore_watch : "\.bash(rc|_(history|logout)),\.(cache|config|gitignore|lesshst|local|npm|pm2|profile|sudo_as_admin_successful|vscode_server),.*swp,snap,.*config.js,package-lock.json,node_modules,jira,logs"
}]
}
When I run pm2 start ecosystem.config.js, my app runs, but constantly restarts with Change detected on path .pm2/.......
I thought that maybe I am not writing the ignore_watch correctly, so I tried
ignore_watch : [
".pm2",
"\.bash(rc|_(history|logout))",
"\.cache|config|gitignore|lesshst|local|npm|pm2|profile|sudo_as_admin_successful|vscode_server)",
".*swp",
"snap",
".*config.js,package-lock.json,node_modules,jira,logs"
]
which then gives me constant restarts with Change detected on path .vscode_server/.......
I then tried the same thing by editing ignore_watch to:
ignore_watch : [
".vscode_server",
".pm2",
but this still restarts with with Change detected on path .vscode_server/.......
I don't understand how the ignore_watch is working, especially where the original pm2 start index.js --watch doesn't appear to watch these folders and its not constantly restarting.
Related
I am running PM2 on windows 10 and have the following as my ecosystem.config.json file use to start the process.
module.exports = {
apps : [{
name : "myApp",
script : "./server.js",
watch : true,
watch_delay: 5000,
ignore_watch: ['node_modules', 'tracking'],
env: {
"NODE_ENV": "production",
}
}]
}
Whenever I update something in the tracking directory is causes a restart. If I move the file location to node_modules and remove tracking from the ignore_watch then it works. I have tried several other paths but anything other than node_modules does NOT seem to work with PM2.
Is the formatting incorrect or am I just missing something or is there a known issue in windows?
PM2 watch and unwatch folder and transpile with babel-node
I was looking for a simple solution for the development server!
Problem: When uploading files to the development server 'express-fileupload' PM2 detects a change in the folders and restarts the process.
I clarify this operation is fine because we had PM2 configured to monitor the entire project!
"ecosystem.config.js" is the configuration; it is simple. You can expand it with the official documentation of PM2. I leave it at the end! How do we fix it, to prevent restarting the process each time we upload a file?
Create the configuration file in the root folder:
ecosystem.config.js
module.exports = {
apps: [
{
name: "DEH",
script: "./server/index.js",
interpreter: "./node_modules/#babel/node/bin/babel-node.js",
interpreter_args: "./node_modules/#babel/node/bin/babel-node",
watch: ["server"],
watch_delay: 1000,
ignore_watch: [
"node_modules",
"./server/src/files",
"./server/src/pdf",
],
watch_options: {
followSymlinks: false,
},
},
],
};
To run it:
pm2 start ecosystem.config.js
Note that ignore_watch has an array with elements that will not be checked.
The interpreter points to the folder where it was installed locally: if you don't specify it you will have to install babel-node globally (I don't recommend it)
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
I have a simple command that works fine when run:
parse-dashboard --config /home/ubuntu/dash/config.json
However, when running it with pm2, it doesn't work:
pm2 start parse-dashboard -- --config=/home/ubuntu/dash/config.json
looking at the logs, i get the error:
node: bad option: --config=/home/ubuntu/dash/config.json
What am I doing wrong?
Use a process file where you specify the arguments. Create the following file and name it for example ecosystem.json (make sure the 'script' and 'cwd' (where the app will be launched) locations are correct for you))
{
"apps" : [{
"name" : "parse-dashboard-wrapper",
"script" : "/usr/bin/parse-dashboard",
"watch" : true,
"cwd" : "/home/parse/parse-dashboard",
"args" : "--config /home/ubuntu/dash/config.json"
}]
}
And run it with
pm2 start ecosystem.json
I want to start node with pm2 and environment variables like --nouse-idle-notification or --max-old-space-size=2048.
However, whatever I do, it is not passing the node variables. I start my app with pm2 and a configuration file. The configuration file looks like:
{
"apps" : [{
"env": {
"NODE_PATH": "/usr/bin/node",
"interpreter_args": "--max-old-space-size=2048 --nouse-idle-notification"
},
"env_development": {
"NODE_ENV": "development"
},
"env_production" : {
"NODE_ENV": "production",
"APP_TYPE": "web"
},
"exec_mode" : "fork",
"name" : "MyApp",
"script" : "/opt/myapp/app.js",
"watch" : false,
"out_file" : "/var/log/app.log",
"error_file" : "/var/log/app.log",
"combine_logs": true,
"node_args": "--max-old-space-size=2048 --nouse-idle-notification",
"args": "--max-old-space-size=2048 --nouse-idle-notification"
}]
}
(as you can see I try to pass in the node variables in multiple ways)
I then start the app with:
pm2 restart pathtojsonfile --env production
Everything is starting up properly and I see variables like "MY_APP" in my code. However, now when I look at the process with "top" I only see:
node /opt/myapp/app.js
When I start the app with forever or manually I can see the process like:
node --max-old-space-size=2048 --nouse-idle-notification /opt/myapp/app.js
Is pm2 just not showing those node arguments or are they really not passed in? (The pm2 started process uses less memory)
Below is extact command to start pm2 with node-args
pm2 start app.js --node-args="--max-old-space-size=4096"
By using "node_args": "--max-old-space-size=2048 --nouse-idle-notification" you did the right thing and these arguments are taken into account.
PM2 renames the process and drop the specified node argument in the process title.