pm2 difference between stop and delete app - node.js

In pm2 node app manager, what is the difference between stop and delete app. I know that delete app deletes the app, from the pm2:s control, but what does stop app do? They both will set node server to offline.
My problem is that during deployment, if I want to pull code, and then restart the node server, then which pm2 commands to use? What I have done now is first pm2 stop app -> pull code -> pm2 start app. But how do I know that the app.js is really updated? What if stop puts the app in memory, and loads it there? So after start, it will start the previous version, and not from the code that was pulled.

Stop command keeps the app in the Apps list, delete command not.
You can see the Apps list with the command:
pm2 status
So if you stopped, you can restart your app just by its name.
I think the command you want is:
pm2 reload [AppName]
Just replace the files and then run the command.
Source:
http://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/
You can handle the reload signal inside you app, what could be interesting in production. More info:
http://pm2.keymetrics.io/docs/usage/signals-clean-restart/

Related

What is the best way to restart a node app automatically on 1) server restart AND 2) changes made to the files?

Apparently:
nodemon helps with changes to the files (e.g. you make changes to index.js)
pm2 and forever keep the app running if you close the terminal, and can restart the app if it crashes.
But: What is the best combination or tool (and how to use it) to
Start the node app automatically on system boot/reboot
Restart the app automatically when changes are done to the files?

Can't stop node.js app started with pm2 process manager

I want to rebuild my node+vue.js app to update the changes I made. The app was started with pm2, but the problem is that I can't see anything when I do pm2 ls. The list is empty. On the other hand, the node app is running. I tried to kill it with pid, but it is restarting immediately.
Any suggestions?

how to deploy expressjs application on window server

There many tools to deploy expressjs application, such as strongloop,pm2.
But they do not work well on windows.
I have install [pm2]2 in my window computer, and start my app.
https://i.stack.imgur.com/ZpOIa.jpg
But when i log off my computer and log in again. the service is disappear.
So I install pm2-windows-service and install pm2 as a window service. It failed again.
Anyone have a good expressjs deployment plan?
You need to make a pm2 save when you started your app. PM2 need to know which apps were started and their state to restart them after a reboot, and thats the aim of the pm2 save command which will save the state of all apps in the pm2 directory.

Nodemon server perpetuality and runtime log issue

I have a sailsjs app on AWS EC2, which I have been running till now using forever. I have two adantages using forever:
1) Perpetuality: I can use the CLI forever start app.js or forever restart app.js and then app starts running and keeps on running till I stop it with the command forever stop app.js. So, the app does not stop even when I close my terminal. The process keeps on running.
2) Runtime Log: I have a .forever directory that has a log file, while on real time records the server logs, and when I check the log using tail -f file_name.log, I get to see run time logs.
However there is a disadvantage: Every time I upload a new/modified server file, I have to restart the app manually. To get rid of this, I am switching from forever to nodemon.
From the documentation provided by Nodemon, I cant figure out how can I replicate the two advantages, as mentioned above, from Nodemon too. Will be a great help if anyone can guide me on how to start my nodejs app using nodemon so that it can keep running even after closing the terminal on my side, and how to watch runtime log of server.
Just my two cents.
I use nodemon daily while developing and I dont think its something you want to use in place of something like forever. Nodemon is used when developing, the software will detect when there has been a file change and restart the server but for deployment it should not be considered.
There is no need to change either because forever has this use case handled with the --w or --watchDirectory comand that will watch for file changes(It can be found here on their readme).

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.

Resources