I need to stop my nodejs app for some conditions.So I have used process.exit()
method. I am running my app with PM2. But PM2 is restarting the app whenever it was stopped. So is there any possibility to disable restarting in PM2.

- 391
- 2
- 3
- 11
-
2Don't run it with PM2 if you don't want it to restart, since that's its sole purpose. – Ben Fortune Dec 15 '15 at 17:04
-
1But by using pm2-gui i am getting some visualized metrics of cpu and memory usage, thats why i am using pm2. – kamesh Dec 15 '15 at 17:11
-
2I assume that the question is to stop restarting the process after some condition inside the application was met (i.e. application stopped processing something... and it's not necessary to do anything more). – jkulak Oct 25 '16 at 01:10
-
So others won't be misled by @BenFortune's comment: no, this is not PM2's sole purpose, at all. (If it were, they wouldn't have a --no-autorestart flag.) PM2 is also used for instrumentation; also it is used to automatically restart node apps on server reboot. Also used to email error notifications for paying customers. Etc. – avocadatoria Feb 22 '23 at 01:17
5 Answers
Simply --no-autorestart
works like charm.
Usage example : pm2 start script.js --no-autorestart

- 109
- 2
- 6
If you are using pm2 using config json file e.g pm2.config.json
or whatever then need to specify similar option "autorestart" to false in your json file.
"autorestart" : false
E.g.
I am using pm2 to run a java application using below json config file
{
"apps" : [
{
"name":"JAVA-Pm2",
"cwd":".",
"script":"/usr/bin/java",
"args":[
"-jar",
"/dir/my-java-app/build/libs/my-java-app-all.jar"
],
"node_args":[],
"exec_interpreter":"",
"exec_mode":"fork",
"autorestart" : false
}
]
}

- 23,445
- 19
- 103
- 133
If you are using the pm2 config file, use
"autorestart" : false
As suggested by @WitVault BUT make sure you don't have the watch
option set to true
like so:
"watch": true // This field should NOT be present, otherwise "autorestart" : false is ignored

- 1,212
- 18
- 25
-
Note: It seems the comment about `watch: true` making `autorestart:false` be ignored is outdated; I have `watch:"**"`, and adding `autorestart:false` successfully caused pm2 to not restart on my process erroring (with the restart-on-file-change still working). – Venryx Dec 22 '21 at 23:05
You can also list exit codes that don't cause pm2 to restart by using the --stop-exit-codes flag at pm2 launch this way you can still use pm2's auto restart feature but have it not restart on a clean exit https://blog.appsignal.com/2022/03/09/a-complete-guide-to-nodejs-process-management-with-pm2.html is a guide on using pm2 with node js you can do a process.stop specifying an exit code that pm2 is told to not restart with I know this is a late response but I added it for future readers of this post

- 17
- 1
- 1
- 5