If I make a node.js code change and upload the file via ftp, the changes don't seem to take effect until I run pm2 stop app and then pm2 start app. I am pretty sure that isn't the way it should work as this means a period of downtime every time you need to make a change. I found this in the documentation:
Hot Reload allows to update an application without any downtime:
pm2 reload all
But wanted to confirm that this is what I need to use?
According to docs. You can do pm2 start app.js --watch.
with --watch flag it will start watching for changes in any file in directory
PM2 can automatically restart your application when a file is modified in the current directory or its subdirectories:
pm2 start app.js --watch
Related
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?
How can I run multiple nodejs applications using command npm start in different directories using pm2 on boot? Also how do i setup them to start again if any error occurs?
I figured out how to start npm start with pm2 using - "pm2 start npm -- start"
but I don't understand how to set up them to boot.
Based on my research "pm2 startup" gives the command to setup startup which I passed as it is in the terminal.
How do I set up the rest?
Thanks,
If you've already got the part where pm2 automatically starts when you reboot, that's good.
You then need to add each of your apps to pm2.
Go into each of your project directories and run
pm2 start your_file.js --name=my_app, where your_file.js is the file you would use to start with regular node. (ie: node server.js, or node index.js, etc.) and my_app is a friendly name that you will see with pm2 status and can use to apply future commands to.
This should start your app. If you run pm2 status it is hopefully in the "running" state.
To save it so that it automatically gets booted when you reboot or restart pm2 you need to run pm2 save whenever you change app configurations.
You can do this for as many apps as you need to start.
I have a pm2 process named app that was used to test the configuration.
I noticed app was starting when the system rebooted, and it was causing errors with the real application.
I ran:
pm2 delete app
then I ran:
pm2 list
and it didn't show app.
When I reboot my system, the app is still there and it is running. I attempted to find information on where the config file is online, and there is no information other than creating a template config file. Where should the config file that pm2 reads on startup be located on an Ubuntu system, or why isn't delete working as I intend? Is there another method or command I can use to remove a pm2 process or am I looking at this incorrectly?
If you use "pm2 delete {appname}" to delete the last app
pm2 delete app
when you run
pm2 save
It will show
[PM2] Saving current process list...
[PM2] Nothing to save !!!
[PM2] In this case we keep old dump file. To clear dump file you can delete it manually !
Which means, actually, the last app information is still not deleted.
The solution is to create a new dump file.
pm2 cleardump
Then, the app will be deleted permanently.
You can check the pm2 file to see what's actually saved into dump file.
/home/ubuntu/.pm2/dump.pm2
Saving some time for those who may or may not use pm2 regularly and struggle when searching for an answer on this:
You have to save the edits for pm2:
pm2 save
after making any changes. Unlike most Unix style settings interfaces, pm2 requires you to save your changes from the running version to the config file.
Hope this helps someone even though it is simple! I don't use PM2 all the time and it was really frustrating to not find any answers on SO or anywhere else that referred to the need to use pm2 save when deleting a process.
I am following pm'2 doc to set it up for a restart on every time a file changes in my app directory. However, it doesen't seem to restart the app when I change the file. The "watching" flag is enabled as well:
Found the solution. For anyone else having this same issue.
For some reason, pm2 needs an app name flag to be passed at the time of it's launch to be able to monitor and restart the service on directory changes.
pm2 start index.js --watch --name=myapp
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/