8

I can spawn a process like:

var spawn = require('child_process').spawn;

var topicListener = spawn('python', ['topic_listener.py','Node.js'], {env: {
    TWITTER_CONSUMER_SECRET: process.env.TWITTER_CONSUMER_SECRET,
    TWITTER_CONSUMER_KEY: process.env.TWITTER_CONSUMER_KEY,
    TWITTER_TOKEN_SECRET: process.env.TWITTER_TOKEN_SECRET,
    TWITTER_ACCESS_TOKEN: process.env.TWITTER_ACCESS_TOKEN
}});

topicListener.stdout.on('data', function (data) {
    console.log(data.toString());
});

topicListener.stderr.on('data', function (data) {
    console.log(data.toString());
});

topicListener.on('close', function (code) {
    console.log("EXITED " + code);
});

So of course I can control it all asycnchronously with .on(close, ...) but is there any other way to control if a process is still alive?

diegoaguilar
  • 8,179
  • 14
  • 80
  • 129

2 Answers2

4
topicListener.on('exit', function (code) { 
 topicListener = null;
 console.log("EXITED " + code);
});

If topiclistener is null, the process is gone

Ben Bieler
  • 1,518
  • 1
  • 14
  • 22
1

spawn('python', ['topic_listener.py','Node.js'].. Return Child process Object. Use topicListener.pid to find unique ID associated with the process if it's alive.

n_rao
  • 155
  • 1
  • 5
  • I already had tested that but even if process was ended pid was still a defined number – diegoaguilar Oct 28 '15 at 05:05
  • 1
    `topicListener.on('close', function (code) { topicListener = null; console.log("closed" + code); }); ` `topicListener.on('exit', function (code) { topicListener = null; console.log("EXITED " + code); });`
    if `topiclistener` is `null` process is absent.
    – n_rao Oct 28 '15 at 05:31