how to detect what triggered PM2 watch restart - node.js

Is there some kind of debug option to find out which file exactly was changed and caused PM2 to restart when it's used with --watch key?

At least in my version 4.5.5 it is quite simple - just show the logs. But the trick is that you should not show the log from the process itself. Therefore just execute pm2 logs without specifying the process.
What you will get is something like this:
PM2 | Change detected on path fallback-backend/logger/logfiles/info.log for app SmartProduction-Fallback - restarting

Related

How to use pm2 with a nodejs app that uses readline for taking command line input?

I have a Node.js app that uses the node's native readline to be able to take command-line inputs.
When launching the app with pm2, the command-line input is unavailable.
Any ideas how to solve this issue? Other than using systemd and creating an init script myself?
Use pm2 to attach to your process and you will see readline, clearline and cursorTo working as expected.
First get your process id with:
$ pm2 id {your-process-name}
[ 7 ]
Let's say it's 7:
$ pm2 attach 7
if you check the pm2 website they clearly mention the following line: Advanced, production process manager for Node.js. So using it in this context is unnecessary as all pm2 does is start your 'node' process and allows you to manage it, the simple way is to use command line args while starting the process.
for example:
I myself use commander for this purpose. it manages all my command line arguments (u can see its usage). and with pm2 i use it like following:
pm2 start server.js --name production -- --env dev -p 3458
notice -- before --env, it is used to separate pm2 arguments from the arguments you want to supply to your process
p.s.
PM2 has more complex usage than this, in the terms of process management, i myself use it for production level deployment. If you want to take input from a user every time s/he starts your app, then you should stick with using node command only

Disable PM2 logging(.pm2/pm2.log NOT .pm2/logs)

I have to run a process with PM2 on a device with very limited disk space, so I need to disable all logging otherwise the device will run out of space after several days.
I'm using:
"out_file": "/dev/null",
"error_file": "/dev/null"
It stops PM2 from creating logs for the process. However, PM2 still creates another log file in .pm2/pm2.log
The size of pm2.log can grow up to 9Mb, which is large for the device.
Is there any way to stop PM2 from creating pm2.log? Or at least some way to clean the log file automatically.
I open an issue at https://github.com/Unitech/pm2/issues/2921
Turns out that pm2.log cannot be disabled in current version of PM2(2.4.6), but the maintainer of PM2 said that this feature will be added in future update.
For now, I'm using the following code to clean log files:
var cmd_flush ='pm2 flush';
var exec = require('child_process').exec;
exec(cmd_flush ,function(error){
debugLog('error: ' + error);
});
It's slow, but at least it works.

PM2 don't restart on clean exit

PM2 has an option to restart an app on error or clean exit. Unfortunately, even clean zero-code exit increases restarts counter. Is there a way to increase restarts counter only on error or crash (non-zero code exit)?
I noticed that last week in some experimenting with PM2 as I look to use it in production this summer. I was NOT able to find a way to have it discern between restart types. The only thing I found was how to reset the restart counter for an app (along with other meta data for the application), like so:
$ pm2 reset <app id>

Correct way to restar/reload application for a different release

I have following folder structure:
current
releases
2192091029019/
1029012901920/
Latest release gets pushed to current folder, and I afterwards start it wiht pm2 start, however If I upload new release with different folder name and do pm2 reload from new folder it still trys to reference original release from where application was started. Is there a way to restart application respecting new code?
I have same problem with this release structure but with supervisord+Rails instead pm2 + node.
In my case i need to completely restart supervisord every deploy to fix that.
So in your case it may work like this:
pm2 stop
kill -SIGTERM {pm2_pid}
pm2 startup
It's hackish but working solution.

Jenkins nodejs pulgin long running running pm2 deamons

At the end of my Jenkins build I use the nodejs plugin to kick off pm2 to spawn a few deamons. When the build finishes I get the message:
Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
Looking at this, and the corrosponding https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller it suggests that I can run something like:
BUILD_ID=dontKillMe pm2 start processes.json
But the deamons are not running after the build completes. Any ideas?
Thanks, Chris

Resources