Saturday, 29 April 2017

How to know if a child procces is finished?

I try to build a bash terminal via web with Node.js and socket.io, and I need know if a command is finished. Some command like 'cd someDirectory' don't emit a output and I don't know if it finished. How to know if a command if finished? If posible set a timeout for each command? I have the following code:

var shell = spawn('/bin/bash');
var stdin = shell.stdin;

shell.on('exit', function() {
  socket.disconnect();
});

shell.on('close',function(){
  console.log('Close');
});

shell['stdout'].setEncoding('ascii');
shell['stdout'].on('data', function(data) {
  socket.emit('stdout', data);
});

shell['stderr'].setEncoding('ascii');
shell['stderr'].on('data', function(data) {
  socket.emit('stderr', data);
});

socket.on('stdin', function(command) {
  stdin.write(command+"\n") || socket.emit('disable');
});

stdin.on('drain', function() {
   socket.emit('enable');
});

stdin.on('error', function(exception) {
  socket.emit('error', String(exception));
});

Thanks in advance.



via Francisco Martin

No comments:

Post a Comment