how to go into a process running in PM2 - node.js

I can see all the processes using pm2 ls, but how can lo into a process and see the logs and make changes?
To make this question pass quality standards I'm adding these unnecessary lines.

Try
pm2 logs "processId here"
or
pm2 monit "processId here"

Related

What is the default location of PM2 log files?

I'm trying to find out where PM2 saves the log files by default?
I'm working with a Linux Ubuntu 16.04 server and I've installed it globally with npm i pm2 -g.
pm2 saves logs to $HOME/.pm2/logs/XXX-err.log by default, where XXX is your pm2 app name
I wanted to see the logs for different processes. There is a console-based UI for this:
pm2 monit
Extra tips for pm2 newbies:
Launch multiple, co-ordinated, instance per cpu core with: pm2 start myApp.js -i max
Beware the 'js' example of cluster configuration; it didn't work for me. Try 'json' instead.
By default, you need to leave user logged in to keep cluster running
Handy commands:
pm2 start all (also stop / delete)
pm2 list
Type pm2 log in a shell and you get an overview of the last 15 log lines and the last 15 error lines in realtime. At the top of these log lines, the location of your logfile is shown. You can also type pm2 log --lines 1000 to show more lines, in this case 1000.
$pm2 log
$pm2 log --lines 500
To exit, just type ctrl-c
A great way to get information on logfile location (and other useful info) is to do a "pm2 describe" on the process that you have running. You can use this method running pm2 as a standard user or if you use it as sudo pm2.
Get the name or id of the process
pm2 list
use describe by either using the id# or the name
pm2 describe 0
pm2 has two types of log files for every app that it runs, an error log file and an out log file.
The error logs are saved to $HOME/.pm2/logs/XXX-error.log or ~/.pm2/logs/XXX-error.log
While the out logs are saved to $HOME/.pm2/logs/XXX-out.log or ~/.pm2/logs/XXX-access.log.
Where XXX is the name of your app.

Amazon EC2 NodeJS server stops itself after 2 days even after using sudo nohup

I have my app running on http://talkwithstranger.com/ and I have deployed it on AWS EC2. I use this command
sudo nohup node index.js &
To continue running my Node JS server even if I close my terminal and exit my SSH.
However, after 2 days everytime I wake up and I find out that the node server itself stops automatically. I checked the running processes by using
ps -ef
and my node script is not there.
Google Chrome say site DNS not found, because NodeJS Express is not running of course to serve my html file, but why it stops itself?
What is causing this unexpected shutdown of my server after every 2 days? I have to manually run nohup again to run it again.
Does nohup has a time to expire or something ?
You should run node.js using a service / process manager. You can use something basic such as forever or supervisord but I would actually advise you to take a look at PM2.
It can do a lot of things - one of them being that it manages your process, makes sure it keeps running, restarts it when it fails, manages the logs, etc. You can also have it autostart when you restart the server.
It becomes really powerful in combination with https://pm2.io, because this enables you to monitor your server's metrics such as CPU and memory remotely and see whether exceptions happened, and much more (such as even remotely updating the software by pulling from git). However, they no longer offer a free plan unfortunately - their plans now start at $79/month, which is a pity. But don't worry, the PM2 application itself is still free and open source, only the monitoring costs money.
Basic usage of PM2:
npm install -g pm2
...to install PM2.
pm2 start my_script.js
Starts a script and lets it run in background.
pm2 status
Shows the status of any running scripts.
pm2 restart all
Restarts all running scripts.
pm2 kill
Stops all scripts and completely shuts down the PM2 daemon.
pm2 monit
Monitors CPU/RAM usage and shows it.
pm2 logs
Shows the last 20 output and error log lines and starts streaming live logs to the console. The logs are stored in the folder ~/.pm2/logs.
Using PM2, your script will not stop - at most, it will restart. And if it does you will be able to more easily understand why because you can easily access logs and watch what happenes with memory usage, etc.
Extra tips:
To avoid filling up the harddisk with logfiles, I recommend installing the module pm2-logrotate:
pm2 install pm2-logrotate
To automatically launch PM2 with the same script on startup when the server starts, you can first save the current configuration:
pm2 save
...and then use the following command to install a startup script - follow the instructions displayed, which will be different based on the exact OS you are using:
pm2 startup
To use PM2 in a more advanced way with multiple processes, custom environment variables, etc., take a look at ecosystem files.
You can try forever.Install using the following command.
npm install -g forever
Then just start forever:
forever start index.js
Another better option for production use is pm2.You can install pm2 with below command
npm install -g pm2
# or
yarn global add pm2
start server
pm2 start index.js
The best thing is you can achieve load balancing with pm2(utilize all available CPU)
pm2 start index.js -i max
For more info, you can visit pm2 documentation page.

pm2 - How to start if not started, kill and start if started

I am trying to start pm2 if it is not running, or kill it and start if it is, how can I achieve this behavior in the WINDOWS command line interface?
There are plenty of solutions using grep in linux but nothing for windows, any idea on how to get this behaviour?
The documentation says that pm2 start -f app.js will kill and start the app but it actually just creates another instance.
Use this:
pm2 delete main.js 2> /dev/null && pm2 start main.js
This part: 2> /dev/null - will simply redirect the stderr to the /dev/null, meaning to nowhere.
It does not seem there is a "single command" way to do this, which is rather important in many development environments, so here are some options:
put soyuka's suggestion on one line.
pm2 stop myprocess; pm2 start myprocess.js
This will output errors, but it will work.
They also have this option built into their ecosystem tools. To use this, go into the folder you are working with and run
pm2 ecosystem
This will generate a file ecosystem.config.js which you will need to make sure your name and script are correct within.
You can then use the command:
pm2 startOrReload ecosystem.config.js
I, however also want to see my logging, so I use this command:
pm2 flush && pm2 startOrReload ecosystem.config.js && pm2 log
This will also flush the logs so you are not seeing old logs.
You can do something like this
pm2 delete your_app_name || : && pm2 start index.js -i 1 --name 'your_app_name'
The : is a null operator that returns 0 success exit code. So whatever happens, pm2 start command will execute (even if pm2 delete fails, for the case where the app does not exist yet).
I'd do this :
pm2 stop myprocess.js #this will just say process not found
pm2 start myprocess.js
Or if you want to clear everything :
pm2 kill
pm2 stop
If you want more advanced possibilities check out the pm2 api.

Pm2 process stops running

I have a node chat application that needs to keep running on my server (ubuntu with nginx). The problem is that the application stops after a few hours or days.
When I check on the server I see that my pm2 list is empty.
The code I use to start my app:
pm2 start notification_server/index.js
It somehow looks as if pm2 is reset after a while. I also tried using forever, but then I run into the same problem. Is there some way to prevent the pm2 list from getting empty?
This is most likely an indication that your server is rebooting. When your server reboots, PM2 shuts down and deletes all Node instances from its "status" list.
You can perform the following steps to make PM2 relaunch your Node programs start back up on reboot:
Run pm2 startup and follow the directions (you will have to perform a sudo command; PM will tell you exactly what to do).
Through pm2 start, get your Node processes up and running just like you like them.
Run pm2 save to register the current state of things as what you want to see on system startup.
Source: http://pm2.keymetrics.io/docs/usage/startup/
Did you try checking logs $ pm2 logs for you application?
Most likely it will tell you why your application was terminated or maybe it just exited as it supposed to. You could find something like that there:
PM2 | App [app] with id [0] and pid [11982], exited with code [1] via signal [SIGINT]
This can tell you what happened. Without more details, it's hard to give you a better answer.

How to reset the id of pm2?

I using pm2. After I started my app several times, the id increased. I deleted all in pm2, and started again, but the id didn't count from 0 any more. How can I reset it?
The solution is to restart pm2, by typing pm2 kill as said by ItalyPaleAle in the comments.
Per pm2's GitHub:
$ pm2 reset <process> # Reset meta data (restarted time...)
Reference: GitHub issue#1456
Looking at the help menu (pm2 --help) it seems like pm2 reset would be the way to go. But that command resets metadata (restarted time, etc) and does not reset IDs. One solution is to use pm2 kill to kill the pm2 daemon and restart it again (thanks, #Yao Zhao). But doing so will stop all current processes. A better approach would be to save the list of current processes, restart pm2, and then restore those processes using the following set of commands.
$ pm2 dump
$ pm2 kill
$ pm2 resurrect

Resources