title: Why the node child spawn process detached from parent process and start running independently ?
body: <p>I have a node child spawn process which is continuously write in to the file using writestream on every "data event" received. The script is run under ssh and facing an edge case problem. </p>
  
  <p>Consider multiple terminals open with same ssh host and the script is start to run in one terminal. Someone accidentally close the ssh terminal during it's execution and doesn't want to stop child process execution. while using a command ps -ef | grep "command name" the child process still running with different parent process id (it shows 1) but the writestream in the child process stops writing to the file. It seems like child process become zombie process eventhough i detached the process from parent. You can find the script below:</p>
  
  <pre><code> var execSpawn = require('child_process').spawn;
  var Promise = require('bluebird');
  
  var spawnAction = function(path, cmd, cb){
      return function(resolve, reject, onCancel){
          cmdExec = execSpawn(path, cmd, {detached: true});
          //cmdExec = execSpawn(path, cmd, {detached: true}).unref();
          var fileData = {}
          var count = 0;
          var stream = fs.createWriteStream('filepath');
          cmdExec.stdout.setEncoding('utf8');
          cmdExec.stdout.on('data', function(data){
              //Certain actions with filedata and count;
              stream.write(data);
          });
          cmdExec.stderr.on('data', function(data){
              //some actions
              stream.write("error");
          });
  
          cmdExec.on('close', function(){
              stream.end();
                  if(cb){
                     resolve(cb(fileData));
                  }else{
                      resolve(count);
                  }
          });
         }
      }
  </code></pre>
  
  <p>This script is running properly when it is allow to run completely without any interruption. When the script execution terminal closes the child process stop the writestream to the file. If i try with detach along with unref() it throws an error like it couldn't figure out the event stdout.on over the child process.  </p>
  
  <pre><code>Cannot read property 'stdout' of undefined  
  </code></pre>
  
  <p>More information during the script running. This is taken in the same host in different terminal</p>
  
  <pre><code> ps -ef | grep command_name
  root     19904 19191  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sdb
  root     19905 19191  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sdc
  root     19906 19191  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sdd
  root     19907 19191  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sde
  root     23101 13105  0 20:16 pts/0    00:00:00 grep --color=auto command_name
  </code></pre>
  
  <p>After closing the script running terminal before it finishes. I am getting this:</p>
  
  <pre><code>ps -ef | grep command_name
  root     19904     1  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sdb
  root     19905     1  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sdc
  root     19906     1  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sdd
  root     19907     1  0 20:16 ?        00:00:00 cli command_name pfitzner7 /dev/sde
  root     23163 13105  0 20:16 pts/0    00:00:00 grep --color=auto command_name
  </code></pre>
  
  <p>I am trying to figure out why it's happening this issue and will it be there any possible ways to do in different way. Why did the parent process id changed to 1 even if it's detached child process? How can a child spawn process can run independently from the parent process?</p>
  
  <p>Please let me know your suggestions on this approach or the reason for the error.</p>
  
  <p>Thanks in advance. </p>