title: Use pm2 start a node app, that node app forks a child process, when the child process is down, Will pm2 restart the node app?
body: <p>Here is my problem that I have met.</p>
  
  <p>There are three files in my node project named <code>app.js</code>, <code>app-8080.js</code>, <code>app-8090.js</code>.</p>
  
  <p>The three files are almost the same except the port.</p>
  
  <p>In <code>app.js</code></p>
  
  <pre><code>
  server.listen(config.port0, () =&gt; {
      console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port0));
  });
  
  </code></pre>
  
  <p>In <code>app-8080.js</code></p>
  
  <pre><code>
  // the above is the same as app.js
  server.listen(config.port1, () =&gt; {
      console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port1));
  });
  
  </code></pre>
  
  <p>In <code>app-8090.js</code></p>
  
  <pre><code>
  // the above is the same as app.js
  server.listen(config.port2, () =&gt; {
      console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port2));
  });
  
  </code></pre>
  
  <p>When I update <code>app.js</code>, I have to do the same work three times.</p>
  
  <p>Now I want to change as below.</p>
  
  <p>In <code>app.js</code></p>
  
  <pre><code>let port = (/^\d+$/.test(process.argv[2])) ? process.argv[2] : config.port0;
  server.listen(port, () =&gt; {
      console.log(utilL.format('app is listening at http://%s:%s', config.host, port));
  });
  </code></pre>
  
  <p>In <code>app-8080.js</code></p>
  
  <pre><code>const childProcess = require('child_process');
  const config = require('./config/config');
  let port = config.port1;
  childProcess.fork('./app.js', [port]);
  </code></pre>
  
  <p>Is that OK? And I use PM2 to start <code>app-8080.js</code></p>
  
  <p>command as below:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ pm2 start app-8080.js
  </code></pre>
  
  <p>When it occurs errors in <code>app.js</code>, will the pm2 restart this process?</p>
  
  <p>And is there any other problem? Or what is the better solution?</p>
  
  <p>I'll be very pleasure with your answer!</p>