pm2 json config not running app properly - node.js

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.

Related

Is there a verbose mode that can be enabled for even more verbose error stacks?

I have a production only error appearing in my pm2 logs that is giving me absolutely no helpful information - the callstack is not deep enough to view where it started in my own codebase, and I know of no environment variables that can be modified for more verbose stack tracing
does anyone know of any settings that can effect the callstack
App [backgroundExcelIntegration:1] exited with code [0] via signal [SIGTERM]
App [backgroundExcelIntegration:1] starting in -fork mode-
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (18583)
at Object.writeFileSync (fs.js:1522:5)
at /usr/lib/node_modules/pm2/lib/God/ForkMode.js:258:12
at wrapper (/usr/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16)
at next (/usr/lib/node_modules/pm2/node_modules/async/waterfall.js:96:20)
at /usr/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
at WriteStream.<anonymous> (/usr/lib/node_modules/pm2/lib/Utility.js:186:13)
at WriteStream.emit (events.js:400:28)
at WriteStream.emit (domain.js:537:15)
at internal/fs/streams.js:340:12
at FSReqCallback.oncomplete (fs.js:180:23)
App [backgroundExcelIntegration:1] online
pid=18547 msg=process killed
Deleting process 1
Stopping app:backgroundExcelIntegration id:1
App [backgroundExcelIntegration:1] exited with code [0] via signal [SIGINT]
pid=18583 msg=process killed
Exited peacefully
the answer ended up having nothing to do with node or typescript, it was a PM2 error, after observing pm2 monit it was discovered the server was crashing every 88 seconds (why 88 i have no idea), seemingly others have experienced similar issue and resolved it by running pm2 update
we ran pm2 update and the issue resolved itself

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.

pm2 --ignore-watch not working

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

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

error when starting the server with grunt or node server

I am trying to use the mean stack as described here : http://mean.io/
after install, when making "grunt", there is an error :
debugger listening on port 5858
4 Jan 01:47:40 - [nodemon] reading ignore list
Express app started on port 3000
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (/mean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (/mean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (/mean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:415:13)
4 Jan 01:47:40 - [nodemon] app crashed - waiting for file changes before starting...
I tried the troubleshooting as specified on the site : Updating Grunt, bower and cleaning npm and bower caches worked well. but updating npm gives an error too:
npm ERR! error rolling back Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm
This looks like a conflict between homebrew and npm.
but anyway i have version 1.3.21 installed.
There is the same error when i am trying node server.
do you have an idea on what i can do to make the server work ?
It may be too easy, but seems like your MongoDB server is not actually running. After having installed Mongo (on your Mac, I assume?), you have to open a Terminal window/tab, run mongod and leave it running (or use screen, etc.). You can try to access MongoDB CLI by typing mongo to make sure the server is running.
#desgnl: if you don't want to run mongod command everytime you are running the grunt, you can always use third party services like MongoDB Hosting: Database-as-a-Service by MongoLab https://mongolab.com Hope this helps too!

Resources