PM2 : NodeJS based process manager won't reboot on file change - node.js

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

Related

Is there a command that allows you to restart a pm2 app if a file in another directory is changed?

I have an Ubuntu server that is running plesk obsidian to manage my websites, on the same server I have a nodejs application running with pm2.
There is a json file that a website and the application needs to access. I am able to make the app's code point to the file and access it, but when the website makes changes to the file it doesn't automatically restart the nodejs app as the file isn't in the same directory.
Is there a command that I can use to make pm2 check for changes with the json file and restart the app?
yes there is a command
pm2 start app.js --watch
here app.js is the file name which cause pm2 to restart

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 deleted process runs on startup

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.

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