13

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.

kamesh
  • 391
  • 2
  • 3
  • 11
  • 2
    Don'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
  • 1
    But 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
  • 2
    I 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 Answers5

41

I think you need the --no-autorestart flag.

"do not automatically restart apps"

http://pm2.keymetrics.io/docs/usage/quick-start/

Jxx
  • 1,014
  • 10
  • 9
7

Simply --no-autorestart works like charm. Usage example : pm2 start script.js --no-autorestart

gilzon.me
  • 109
  • 2
  • 6
5

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
  } 
  ]
}
WitVault
  • 23,445
  • 19
  • 103
  • 133
2

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
Cels
  • 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
0

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

Matthew Baker
  • 17
  • 1
  • 1
  • 5