pm2 --ignore-watch not working - node.js

I have been using pm2 for some time now. Recently, I needed to add a custom log directory to my Express4 project called "actionLog". Since it is a directory that gets updated with log files and I don't want pm2 to restart the app on log file changes, I wanted pm2 to ignore watching that directory. After updating pm2 to latest, here is the command I used:
pm2 start app.js --watch --ignore-watch="actionLog"
I get the following error streaming through the pm2 logs:
PM2 Error: watch ENOSPC
PM2 at exports._errnoException (util.js:746:11)
PM2 at FSWatcher.start (fs.js:1172:11)
PM2 at Object.fs.watch (fs.js:1198:11)
PM2 at createFsWatchInstance (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:37:15)
PM2 at setFsWatchListener (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:80:15)
PM2 at EventEmitter.NodeFsHandler._watchWithNodeFs (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:228:14)
PM2 at EventEmitter.NodeFsHandler._handleFile (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:255:21)
PM2 at EventEmitter.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:468:21)
PM2 at FSReqWrap.oncomplete (fs.js:95:15)
PM2 Error: watch ENOSPC
PM2 at exports._errnoException (util.js:746:11)
PM2 at FSWatcher.start (fs.js:1172:11)
PM2 at Object.fs.watch (fs.js:1198:11)
PM2 at createFsWatchInstance (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:37:15)
PM2 at setFsWatchListener (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:80:15)
PM2 at EventEmitter.NodeFsHandler._watchWithNodeFs (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:228:14)
PM2 at EventEmitter.NodeFsHandler._handleFile (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:255:21)
PM2 at EventEmitter.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:468:21)
PM2 at FSReqWrap.oncomplete (fs.js:95:15)
PM2 Error: watch ENOSPC
I have also tried using the command:
pm2 start bin/www --watch --ignore-watch="actionLog"
This also generated the same error.
Once I have the correct ignore-watch parameter, I will update the json config file I use for starting pm2. At the moment, using this config file with the ignore-watch is also causing errors, but instead of a detailed stack trace as above, I only see the following in the pm2 logs:
PM2: 2016-02-23 04:05:34: Starting execution sequence in -fork mode- for app name:aadm id:2
PM2: 2016-02-23 04:05:34: App name:aadm id:2 online
PM2: 2016-02-23 04:05:35: Change detected on path actionLog/userAction.log for app aadm - restarting
PM2: 2016-02-23 04:05:35: Stopping app:aadm id:2
PM2: 2016-02-23 04:05:35: App name:aadm id:2 exited with code SIGTERM
PM2: 2016-02-23 04:05:35: Process with pid 5102 killed
PM2: 2016-02-23 04:05:35: Starting execution sequence in -fork mode- for app name:aadm id:2
PM2: 2016-02-23 04:05:35: App name:aadm id:2 online
I have looked at some of the reports of ignore-watch problems such as:
https://github.com/Unitech/PM2/issues/1288
https://github.com/Unitech/PM2/issues/918
https://github.com/Unitech/PM2/issues/1275
How to auto reload project nodejs use pm2
Express 4 + pm2 watch not working
Unfortunately, I am still stuck. Any ideas?

You have two options:
Increase the systems inotify max watch limit:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
or
Try to decrease you watch count by additionally ignoring node_modules:
pm2 start bin/www --watch --ignore-watch="actionLog node_modules"
If you don't have sudo rights option 2 is for you.

Turns out even though the errors were triggered by ignore-watch, they were not caused by it. The following command fixed the issue:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
I found this solution here: https://stackoverflow.com/a/32600959/2234029
I also tried "npm dedupe" -as suggested on that thread- which didn't help.

I had the same problem.
It was driving me crazy, then I discovered there were many sessions of PM2 running in my system.
A command like ps aux | grep pm2 shows you all instances running.
So I killed manually each instance (shown by the command above) one by one with:
kill -9 <pid1> <pid2> <pid3> <...>
At last I restarted the process:
pm2 start ecosystem.config.js
and then everything went working smoothly as written in my config file (without any "watch" option).
The actual question is: why those processes where running on my server, but I can't answer this.

I think it is ignore_watch ie with an underline

Related

PM2 cluster mode multi instance not working (with Next.js server) [duplicate]

Nodejs was running on PM2 for a long time. And there is a corn which clears PM2 logs everyday
0 0 * * * find /home/user/.pm2/logs* -mtime +2 -exec rm -rf {} \;
Below error occurred for 1000 times and then pm2 stopped working and then when I reloaded the instance it was working fine as usual.
What could be the reason for this error?
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
throw err;
^
[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/user/.pm2/logs/out.log'
}
2020-09-09T00:55:33: PM2 log: App name:app id:1 disconnected
2020-09-09T00:55:33: PM2 log: App exited with code [1] via signal [SIGINT]
2020-09-09T00:55:33: PM2 log: App starting in -cluster mode-
2020-09-09T00:55:33: PM2 log: App online
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
throw err;
^
[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/user/.pm2/logs/out.log'
}
2020-09-09T00:55:33: PM2 log: App name:app id:1 disconnected
2020-09-09T00:55:33: PM2 log: App exited with code [1] via signal [SIGINT]
2020-09-09T00:55:33: PM2 log: App starting in -cluster mode-
2020-09-09T00:55:33: PM2 log: App online
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
throw err;
^
[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/user/.pm2/logs/out.log'
}
You're trying to clean up logfiles by just using rm . You Can't Do Thatâ„¢. On Linux and other UNIX-derived OSs, the program writing the log file holds it open even when you rm it from a directory. The file doesn't actually disappear until the program writing it closes it (or terminates). This is true for all sorts of software, not just pm2.
If all you want is to clear out the log files and start over, the command pm2 flush is for you.
If you want to save old logs for a limited period of time, you should investigate pm2's log rotation addon.
Commands like this may do the trick for you
# install the pm2 addon ... notice it says pm2 install, not npm install
pm2 install pm2-logrotate
# rotate when a logfile fills to ten megabytes
pm2 set pm2-logrotate:max_size 10M
# gzip compress the rotated logs to save space
pm2 set pm2-logrotate:compress true
# force rotation at 00:00 each day even if logfile is not full
pm2 set pm2-logrotate:rotateInterval '0 0 * * *'
You can say pm2 config pm2-logrotate to get it to show you its current settings.
Can you try the below two steps and check this if it works.
pm2 start bin/www -i 0 // this will start a cluster and the main app
pm2 stop 0 // this will allow the cluster to keep running
Adding
out_file: "/dev/null",
error_file: "/dev/null"
to the ecosystem.config.json file worked for me. It is basically disabling the logs. This is good for testing if the reason is in the logging functionality.
This is a workaround. If it works the the rotation of logging suggested in the answer of #O.Jones should work too.

Getting errors when trying to start app via yarn & pm2

Receiving an error when attempting to start node (next.js) app via yarn and pm2. My google fu didn't turn up anything useful. I suspect the issue is with nvm, though I don't have enough experience to understand how to fix it.
Ubuntu 20.04
nvm 0.38.0
node v14.5.0
yarn 1.22.10
pm2 4.5.6
ecosystem.config.js
module.exports = {
apps: [
{
name: "next",
script: "yarn",
interpreter: "bash",
args: "start:next",
instances: 1,
env: {
NODE_ENV: "development",
},
env_staging: {
NODE_ENV: "production",
},
env_production: {
NODE_ENV: "production",
}
}
]
}
Starting file via:
pm2 start ecosystem.config.js --env staging
or
pm2 start yarn --interpreter bash --name next -- start:next
Receiving errors:
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/home/deploy/.pm2/pm2.log last 15 lines:
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: Script /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn had too many unstable restarts (16). Stopped. "errored"
/home/deploy/.pm2/logs/express-out.log last 15 lines:
/home/deploy/.pm2/logs/express-error.log last 15 lines:
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 7: var: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: syntax error near unexpected token `('
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: `var majorVer = parseInt(ver.split('.')[0], 10);'
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 7: var: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: syntax error near unexpected token `('
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: `var majorVer = parseInt(ver.split('.')[0], 10);'
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found
--interpreter bash is your problem. yarn is not a bash script, rather what appears to be a JS program. Do this:
pm2 start yarn --name appname -- start:next
I found the only solution was to run it by defining a specific port, in my case 3000 thanks to the comment of Brandon Aguilar. That's the command
pm2 start yarn --name appname -- start -p 3000
It seems that by default it runs it to port 80

PM2 with NodeJs crashing then restart every 1 second

I have a NodeJs+Express+Mongodb app and I'm trying to run it on my vps. There was a crashing problem with mongo so I removed "mongo.conf" file. Usually my app start crashing after few minutes. But after this change, everything was good for 24h then my server crash and restart every 1 second :
pm2.log (loop)
2021-04-25T09:46:58: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:46:58: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:46:58: PM2 log: App [server:1] online
2021-04-25T09:47:00: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:47:00: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:47:00: PM2 log: App [server:1] online
2021-04-25T09:47:02: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:47:02: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:47:02: PM2 log: App [server:1] online
2021-04-25T09:47:34: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:47:34: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:47:34: PM2 log: App [server:1] online
server-error.log (loop)
Deprecation warning: use moment.updateLocale(localeName, config) to change an existing locale. moment.defineLocale(localeName, config) should only be used for creating a new locale See http://momentjs.com/guides/#/warnings/define-locale/ for more info.
(node:1022) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
TypeError: Cannot read property 'db' of undefined
at /home/debian/app/server.js:987:21
at /home/debian/app/node_modules/mongodb/lib/utils.js:684:9
at /home/debian/app/node_modules/mongodb/lib/mongo_client.js:226:23
at connectCallback (/home/debian/app/node_modules/mongodb/lib/operations/connect.js:366:5)
at /home/debian/app/node_modules/mongodb/lib/operations/connect.js:599:14
at Object.connectHandler [as callback] (/home/debian/app/node_modules/mongodb/lib/core/sdam/topology.js:285:11)
at Timeout._onTimeout (/home/debian/app/node_modules/mongodb/lib/core/sdam/topology.js:443:25)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
I don't understand what's wrong. Everything works fine on local. Every page works great online and suddenly everything crash and start reboot in loop ?
If that is an environmental variable not seen by your server application, try restart by refreshing with the following
pm2 restart server --update-env

pm2 json config not running app properly

Trying to get a nodejs app up and running with pm2.
We use SASS and rebuild the to css files on file change with "node-sass-middleware". It all works perfectly until pm2 starts auto restarting the app every time the sass middleware rebuilds a css file... which, is what we tell it to do with:
pm2 start app.js --watch
So, after some reading of the docs it seems you are meant to be able to configure pm2 with a json file and it has an option of "ignore_watch".
So, following the guide on github: https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#json-app-declaration we set a config json file up to look as follows:
{
"name" : "ourApp",
"script" : "server.js",
"watch" : true,
"ignore_watch" : ["public/css"],
"env": {
"NODE_ENV": "development"
}
}
The problem is, as soon as we try to run it the pm2 logs spit out 1000's of lines of log errors all looking a little like this:
PM2: 2015-05-09 15:32:00: Error: watch ENOSPC
PM2: at errnoException (fs.js:1024:11)
PM2: at FSWatcher.start (fs.js:1056:11)
PM2: at Object.fs.watch (fs.js:1081:11)
PM2: at createFsWatchInstance (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:24:15)
PM2: at setFsWatchListener (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:47:19)
PM2: at EventEmitter.NodeFsHandler._watchWithNodeFs (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:177:15)
PM2: at EventEmitter.NodeFsHandler._handleFile (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:201:8)
PM2: at EventEmitter.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:353:12)
PM2: at Object.oncomplete (fs.js:107:15)
PM2: 2015-05-09 15:32:00: Error: watch ENOSPC
PM2: at errnoException (fs.js:1024:11)
PM2: at FSWatcher.start (fs.js:1056:11)
PM2: at Object.fs.watch (fs.js:1081:11)
PM2: at createFsWatchInstance (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:24:15)
PM2: at setFsWatchListener (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:47:19)
PM2: at EventEmitter.NodeFsHandler._watchWithNodeFs (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:177:15)
PM2: at EventEmitter.NodeFsHandler._handleFile (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:201:8)
PM2: at EventEmitter.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:353:12)
PM2: at Object.oncomplete (fs.js:107:15)
PM2: 2015-05-09 15:32:00: Process with pid 9155 still not killed, retrying...
PM2: 2015-05-09 15:32:00: Process with pid 9155 still alive after 800ms
thanks to the answer below
This seems to be the culprit and apparently in linux world ENOSPC means no disk space left.. but with only 2.1gig being used on the entire dev server with a total HDD space of 25gig this cannot be the issue.
PM2: 2015-05-09 15:32:00: Error: watch ENOSPC
Further more, when we run pm2 from cli as mentioned at the top is all works perfectly...
We really need this to work as it is causing havoc for the dev team.
Has anyone else hit this issue?
Thanks,
John
ENOSPC means no space left on the device. So check to see if one of your file systems is full. Assuming your on Linux or another Unix-like operating system, /tmp and /var are likely candidates.

pm2 loop restarting with sailsjs

I have a problem with pm2.
My start json is
{
"apps" : [{
"name" : "sails",
"script" : "app.js",
"log_date_format" : "DD-MM-YYYY HH:mm Z",
"ignoreWatch" : ["[\\/\\\\]\\./", "node_modules", ".tmp", "views", "assets", ".idea"],
"watch" : true
}]
}
Then i do
pm2 start dev.json
PM2 goes to infinity restarting with this message
PM2: 2015-02-11 14:56:39: Change detected for app name: sails - restarting
PM2: 2015-02-11 14:56:39: Process with pid 5766 still not killed, retrying...
PM2: 2015-02-11 14:56:39: App closed with code: 0
PM2: 2015-02-11 14:56:39: App name:sails id:0 exited
PM2: 2015-02-11 14:56:40: Process with pid 5766 killed
PM2: 2015-02-11 14:56:40: Starting execution sequence in -fork mode- for app name:sails id:0
PM2: 2015-02-11 14:56:40: App name:sails id:0 online
But no files was changed. It seems to be PM2 restarting after sails log out debug messages. Any ideas?
I'm solved this issue by switching a PM2 version frpm 0.12.5 to 0.11
I had a similar issue. Install the newest version that was released 3 days ago, 0.12.7 and see if the information in issue 969 link helps you out.
https://github.com/Unitech/PM2/issues/969

Resources