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

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.

Related

nohup npm start terminated

I am trying to run my reactjs project on background with this command.
nohup npm start > /dev/null 2>&1 &
output should be redirected and nohup means do not terminate process when the stty is cut off.
However my npm node is not running at all. this is from nohup log.
Starting the development server...
events.js:291
throw er; // Unhandled 'error' event
^
Error: EBADF: bad file descriptor, read
Emitted 'error' event on ReadStream instance at:
at internal/fs/streams.js:202:14
at FSReqCallback.wrapper [as oncomplete] (fs.js:528:5) {
errno: -9,
code: 'EBADF',
syscall: 'read'
}
you can use forever. you can check more advanced scenarios from the link. below is the simplest way of using it
sudo npm install forever -g
then
forever start app.js

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

error starting pm2 in cluster mode in windows

I am getting an error while starting pm2 in cluster mode.
I ran this command -
pm2 start <filename> -i 4
Only one instance gets created.
I am using Windows 7, Node 0.12.0 and pm2 0.14.0
This is my error log
2015-06-18 10:52:04: [PM2][WORKER] Started with refreshing interval:
30000 2015-06-18 10:52:04: [[[[ PM2/God daemon launched ]]]]
2015-06-18 10:52:04: BUS system [READY] on port \.\pipe\pub.sock
2015-06-18 10:52:04: RPC interface [READY] on port \.\pipe\rpc.sock
2015-06-18 10:52:04: Starting execution sequence in -cluster mode- for
app name:server id:0 2015-06-18 10:52:04: App name:server id:0 online
2015-06-18 10:52:04: Starting execution sequence in -cluster mode- for
app name:server id:1 2015-06-18 10:52:04: Trace: { [Error: spawn
EBADF] code: 'EBADF', errno: 'EBADF', syscall: 'spawn' }
at Object.God.logAndGenerateError (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\Methods.js:30:15)
at Object.nodeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\ClusterMode.js:52:11)
at Object.executeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:145:9)
at ex (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:361:18)
at C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:365:16
at Worker.cluOnline (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:188:17)
at Worker.g (events.js:199:16)
at Worker.emit (events.js:104:17)
at online (cluster.js:449:12)
at Worker.onmessage (cluster.js:436:7) 2015-06-18 10:52:04: Starting execution sequence in -cluster mode- for app name:server id:2
2015-06-18 10:52:05: Trace: { [Error: spawn EBADF] code: 'EBADF',
errno: 'EBADF', syscall: 'spawn' }
at Object.God.logAndGenerateError (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\Methods.js:30:15)
at Object.nodeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\ClusterMode.js:52:11)
at Object.executeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:145:9)
at ex (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:361:18)
at C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:362:25
at nodeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:146:29)
at Object.nodeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\ClusterMode.js:53:14)
at Object.executeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:145:9)
at ex (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:361:18)
at C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:365:16
2015-06-18 10:52:05: Starting execution sequence in -cluster mode- for
app name:server id:3 2015-06-18 10:52:05: Trace: { [Error: spawn
EBADF] code: 'EBADF', errno: 'EBADF', syscall: 'spawn' }
at Object.God.logAndGenerateError (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\Methods.js:30:15)
at Object.nodeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\ClusterMode.js:52:11)
at Object.executeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:145:9)
at ex (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:361:18)
at C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:362:25
at nodeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:146:29)
at Object.nodeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God\ClusterMode.js:53:14)
at Object.executeApp (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:145:9)
at ex (C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:361:18)
at C:\Users\pbaid\AppData\Roaming\npm\node_modules\pm2\lib\God.js:362:25
I met this problem today too.
If I run "pm2 logs" before any other pm2 operation, and then start applications in cluster mode ("pm2 start app.js -i 4"), everything looks fine. It seems pm2 need some time for initialization. I have no idea about the real reason.
Looking forward for better solution too.
The solution is quite simple: restart the pc or server.
On Windows, don't use clusters:
pm2 start "filename.js"

foreverjs turns process in STOPPED state instead of restarting

I have nodejs process that listens for an connection. I want to let foreverjs restart it on any connection error this way:
amqpConnection.on('error', function (err) {
console.error(err);
process.exit(1);
});
But this don't work. After error I see my process in this state:
~> forever list
info: Forever processes running
data: uid command script logfile
data: [0] OpQF /usr/bin/node my-script.js my-script-log.log STOPPED
Log file has connection error and two attempts to restart script.
error: Script restart attempt #2
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
error: Forever detected script exited with code: 1
Why forever stops trying to restart my process?
I should have read warnings that forever prints!
Script that not stay running more than --minUptime (1000ms by default) considered "spinning". One should set --spinSleepTime to make spinning scripts to be restarted. When this parameter is omitted spinning script won't be restarted.
I have reproduced your problem and found that forever successfully restart your process if some delay (1sec) take place before process.exit.
It's look that forever don't restart process when it crash too fast.
setTimeout(function() {
process.exit(1);
}, 1000);

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.

Resources