jhrwekuh
jhrwekuh

Reputation: 326

PM2, exit with code 0 (restarts on start)

I tried to install PM2 on my app, and it works perfectly fine on localhost. However the problem occurs, when I am moving app into the docker container and try to run on the Linux machine.

I am running command npm run production, which executes following code:

pm2 start ecosystem.config.js --env production --no-autorestart

Thereafter, on the command line I see how PM2 boots and start my app on all available cores.

app         | [PM2] PM2 Successfully daemonized
app         | [PM2][WARN] Applications App not running, starting...
app         | [PM2] App [App] launched (2 instances)
app         | ┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
app         | │ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
app         | ├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
app         | │ 0   │ App    │ default     │ 1.0.0   │ cluster │ 36       │ 0s     │ 0    │ online    │ 0%       │ 38.0mb   │ root     │ disabled │
app         | │ 1   │ App    │ default     │ 1.0.0   │ cluster │ 43       │ 0s     │ 0    │ online    │ 0%       │ 25.0mb   │ root     │ disabled │
app         | └─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

And then it returns a message:

app exited with code 0

The whole process lopps over again and again and again... I tried to run it on 'cluster_mode' on 'fork', I tried to run it with --no-autorestart but it still keeps restarting.

I literally have no idea what am I doing wrong?

@@EDIT, added ecosystem.config.js


module.exports = {
  apps : [{
    name: 'App',
    script: './build/server/index.js',

    // Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
    exec_mode: 'cluster_mode',
    args: 'none',
    instances: os.cpus().length,
    autorestart: false,
    watch: false,
    // max_memory_restart: '3G',
    env: {
      NODE_ENV:'development',

    },
    env_production: {
      NODE_ENV:'production',

    }
  }],

  deploy : {
    production : {
      user : 'szygendab',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : '/var/www/production',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

Upvotes: 4

Views: 3443

Answers (2)

Daniel Brown
Daniel Brown

Reputation: 161

In my case I had several PM2 apps running fine, but moved to a new file structure that I was new to. npm start always worked ok because it was pointing the the proper file. The proper file in my situation means that there was a line of code in there to tell the server to listen to port whatever. I assumed my new file structure worked the same as my old one (I stepped away from development for a few years since I have other businesses to run). I was telling PM2 to start app.js in the root server directory, but there was no call in app.js to actually run the server. Very big duh, but when you are rusty and want to start a new project, this is when these mistakes happen. Just to simplify things, I used a process.json file in the server root directory and pointed to the appropriate file. In my case it read like this:

enter image description here

Upvotes: 2

jhrwekuh
jhrwekuh

Reputation: 326

UPDATE

I found the solution. Basically the application on docker should not be run starting with pm2, but with pm2-runtime. That is the reason why it was working on localhost and not inside the docker image. From what I understand, docker completed a task and closed it, thereafter it kept receiving another starting tasks and was looping over starting the app.

Instead pm2 start .... I have switched method for test instance to pm2-runtime start ... and it helped.

Upvotes: 5

Related Questions