Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a Node spawned process is still running?

Tags:

node.js

spawn

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?

like image 772
diegoaguilar Avatar asked Oct 27 '15 18:10

diegoaguilar


1 Answers

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

If topiclistener is null, the process is gone

like image 168
Ben Bieler Avatar answered Oct 20 '22 18:10

Ben Bieler