2012-07-13 3 views
1

노드를 사용하여 특정 bash 프로세스의 출력을 시작하고 계속 캡처 할 수 있습니까? 예를 들어, 내가 tail /some/file을 실행했다고 가정 할 때, 출력 할 때마다 새로운 라인을 듣고 계속 듣고 있습니까?NodeJS에서 bash 출력 캡쳐

+0

글쎄, node.js에서 처리 할 수 ​​있는지 확실하지 않은 경우 프로세스의 표준 출력에 연결해야합니다. – Bobby

+0

@Bobby 네, 사실 아주 쉽게 할 수 있습니다. 'child_process' 모듈을보십시오. :) –

답변

6

입니다.

child_process.spawn을 사용하여 프로세스를 생성하고 출력을 모니터링 할 수 있습니다. 그러나 꼬리, 고양이 등과 같이 길거나 연속적으로 실행되지 않는 명령의 경우 child_process.exec을 사용하면 stdoutstderr의 전체 출력을 캡처하여 한꺼번에 출력 할 수 있습니다.

var cp = require("child_process"); 

cp.exec("tail /some/file", function (err, stdout, stderr) { 
    // If an error occurred, err will contain that error object 
    // The output for the command itself is held in stdout and stderr vars 
}); 
8
var spawn = require('child_process').spawn, 
    tail = spawn('tail', ['-f', '/tmp/somefile']); 
tail.stdout.pipe(process.stdout); 

child_process 모듈은 나뿐만 아니라이 대답을 추가 한, 완성도를 들어 well documented

+0

['exec'] (http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)을 실행할 수도 있습니다. 그러면 stdout과 stderr의 전체 출력을 수집합니다. '꼬리 (tail) '와 같은 인스턴스의 경우 잘 작동합니다. (계속 실행중인 스크립트가있는 경우 출력 스트림을 모니터링하는 것이 더 좋습니다.) –

+0

제 스크립트는 연속적으로 사용됩니다 (예 :'tail' 또는 Google App Engine의'dev_appserver.py'). 나는 이것을 시험 할 것이고 당신에게 돌아갈 것이다. –

+0

이 작업은 Windows에서 가능합니까? – ANinJa