|
#!node |
|
var util = require('util'); |
|
var spawn = require('child_process').spawn; |
|
|
|
//console.log(process.argv); |
|
var action = process.argv[2]||'py'; |
|
//console.log('action =', action); |
|
var child; |
|
if(action === 'py') { |
|
var proc = 'python'; |
|
var args = ['child.py']; |
|
if(process.platform == 'win32') { |
|
proc = 'py'; |
|
args = ['-2', 'child.py']; |
|
//args = ['-3', 'child.py']; |
|
} |
|
//console.log(proc, args); |
|
child = spawn(proc, args, {stdio:[0, 1, 2, 'ipc']}); |
|
} else if(action === 'js') { |
|
child = spawn(process.execPath, ['child.js'], {stdio:[0, 1, 2, 'ipc']}); |
|
} else if(action === 'pfiles') { |
|
child = spawn(action, [process.pid], {stdio:[0, 1, 2, 'ipc']}); |
|
} else if(action === 'pfiles1') { |
|
child = spawn('sh', ['-c', 'exec pfiles $$'], {stdio:[0, 1, 2, 'ipc']}); |
|
} else { |
|
child = spawn(action, [], {stdio:[0, 1, 2, 'ipc']}); |
|
} |
|
//child.stdout.on('data', function (data) { |
|
// console.log('stdout: ' + data); |
|
//}); |
|
//child.stderr.on('data', function (data) { |
|
// console.log('stderr: ' + data); |
|
//}); |
|
child.on('close', function (code) { |
|
console.log('child process exited with code ' + code); |
|
}); |
|
child.on('error', function (err) { |
|
console.log('child process not started with error ' + err); |
|
}); |
|
child.on('message', function(json) { |
|
console.log('child->parent message passed', json); |
|
}); |
|
//console.log('child', child); |
|
//console.log('child.stdio', child.stdio); |
|
child.send({type:'intro'}); |
|
setTimeout(function() { |
|
child.send({type:'exit'}); |
|
}, 1500); |