PM2 deleted process runs on startup - node.js

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.

Related

Run 'pm2 save' after the application is restarted from the --watch flag

So I'm currently running a pm2 application with the watch flag set to true so my application restarts whenever a file is changed. But the process list is never saved (i.e. 'pm2 save' is never ran). I need to current process list to be saved after a file is updated so on machine reboot, the most recent version of the pm2 service is started.
So how do I make it so 'pm2 save' is ran after the watch flag restarts the service? Or am I misunderstanding 'pm2 save' and that doesn't actually need to be ran after each code change.
Turns out the command pm2 set pm2:autodump true was recently added to PM2 that will auto save PM2 on restart/start.
Its not really documented anywhere unfortunately. Hope this helps some people

Make changes without stopping and starting pm2

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

PM2 : NodeJS based process manager won't reboot on file change

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

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).

pm2 difference between stop and delete app

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/

Resources