nodejs / node-v0.x-archive

Moved to https://github.com/nodejs/node
34.44k stars 7.31k forks source link

child_process spawn #14133

Closed gobwas closed 9 years ago

gobwas commented 9 years ago

Hello!

How can I be noticed about successful spawning with child_process.spawn() ?

Example:


var spawn = require("child_process").spawn,
      commandBad, commandGood;

commandBad = spawn('bad_command');
commandBad.on('error', function(err) { console.log("Got error, OK"); });

commandGood = spawn("whoami");
commandGood.on('???', function() { console.log("Spawn successed!"); }); // how can i do this?

Thanks!

CGavrila commented 9 years ago

Is this what you are looking for?

commandGood.on('close', function (exitCode) {
  if (code !== 0) {
    console.log('ps process exited with code ' + code);
  } else {
    console.log('Process was successful');
  }
});
gobwas commented 9 years ago

Hi, @CGavrila, no )

Im looking to some event, when I can decide to commandGood.stdin.write(...).

CGavrila commented 9 years ago

@gobwas: there may not be a way of doing this, as far as I know. You will get an error though if the spawn is not successful, so you could use that in order to stop the stdin.write.

dayuoba commented 9 years ago

no error,means it is running,and if you want to handle the process forked,

fooYouSpawned.stdout.on('data', function(data) {});

may be helpful.other events like exit,error.Just deal with the life cycle of porcess But i think it is not a node issue.

gobwas commented 9 years ago

@dayuoba data event will be fired just if there is some data to out by spawned process =)

May be, yes, and this is not a node issue. But by the way, it could be useful thing.