title: How can I restart a Node.js child process
body: <p>I'm trying to restart a certain <code>child_process</code> by the name of <code>serverSpawnProcess</code>. I can get it to stop, but I can't start it again.</p>
  
  <pre><code>serverSpawnProcess.stdin.write('stop\n');
  serverSpawnProcess = null;
  setTimeout(function() {
    serverSpawnProcess = spawn('java', [
      '-Xmx512M',
      '-Xms512M',
      '-jar',
      'server_files/minecraft_server.jar',
      'nogui'
    ]);
    response.send('r');
  }, 10000);
  </code></pre>
  
  <p>is how I thought I'd go about it, but it only stops the server, it won't spawn it again. Node.js 5.3.0 if that helps.</p>
  
  <p><strong>EDIT</strong> For the sake of anyone else looking for how to do this, I ended up putting the spawn into a function, which I call like so</p>
  
  <pre><code>serverSpawnProcess.on("close", function() {
      response.send('r2');
      // Wait for process to exit, then run again
  
      startServer();
   });
  </code></pre>