danday74
danday74

Reputation: 56986

How to give up if a process won't start in pm2?

On Windows, pm2 is starting my app on 8 processes.

My app is not starting (due to a port conflict).

So pm2 retries again and again and forever, which on linux is fine but Windows is a pain due to all the terminal Windows that spawn and close and re-spawn.

How do I tell pm2 to give up if it can't start the app after 20 seconds? Or even better, how do I tell it not to try again if it falls over within the first 10 seconds?

Upvotes: 0

Views: 2259

Answers (1)

danday74
danday74

Reputation: 56986

After a big search I found the docs here:

http://pm2.keymetrics.io/docs/usage/application-declaration/#control-flow

This details flags such as:

min_uptime - min uptime of the app to be considered started

max_restarts - number of consecutive unstable restarts (less than 1sec interval or custom time via min_uptime) before your app is considered errored and stop being restarted

restart_delay - time to wait before restarting a crashed app (in milliseconds). defaults to 0.

my config yaml file (forever.yaml) now looks like this:

# pm2 stop|start|restart|reload|delete pm2.yaml # stop|start|restart|reload|delete all
# example - pm2 start forever.yaml
#           pm2 stop forever.yaml --only mock-be
# reload is a no downtime restart

apps:

  - name: mock-be
    script: ./mbe/mockBEServer.js
    instances: 2
    exec_mode: cluster
    watch: true
    # see http://pm2.keymetrics.io/docs/usage/application-declaration/#control-flow
    max_restarts: 5 # number of consecutive unstable restarts
    min_uptime: 5000 # min uptime of the app to be considered started
    restart_delay: 1000 # time to wait before restarting a crashed app

  - name: node-mt
    script: ./nmt/nodeMTServer.js
    instances: 2
    exec_mode: cluster
    watch: true
    max_restarts: 5
    min_uptime: 5000
    restart_delay: 1000

start with ..

pm2 start forever.yaml

This config will attempt 5 consecutive unstable restarts before putting the process in an errored state. When in an errored state a process will no longer try to restart. A restart is considered unstable if the uptime is less than 5 seconds (this might happen, for example, if a port conflict occurs).

Upvotes: 1

Related Questions