node.js:使用exec调用的外部脚本会在我使用spawn的时候提前终止

当我使用节点来启动我的扫描仪时,它可以完美地工作:

var exec = require("child_process").exec; function scanner(response) { console.log("Request handler 'scanner' was called."); exec("scanimage --source 'Flatbed' -pv --format tiff --batch=$(date +%Y%m%d_%H%M%S)_p%04d.tiff --resolution 300 --batch-count=1", function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); response.end(); }); } 

扫描工作,这个输出写成:

 Request handler 'scanner' was called. stdout: stdout: Scanning 1 pages, incrementing by 1, numbering from 1 Scanning page 1 scanimage: scanning image of size 2552x3508 pixels at 24 bits/pixel scanimage: acquiring RGB frame scanimage: min/max graylevel value = 15/255 scanimage: read 26857248 bytes in total Scanned page 1. (scanner status = 5) 

由于扫描需要一段时间,特别是对于许多页面,我想实时捕捉标准输出以在Web应用程序中显示进度条或类似内容。 所以我试图用spawn得到控制台输出:

 function scanner(response) { var exec = require("child_process").exec; console.log("Request handler 'scanner' was called."); var spawn = require('child_process').spawn, scanimage = spawn('scanimage', ['--source', 'Flatbed', '-pv', '--format', 'tif','--batch=$(date +%Y%m%d_%H%M%S)_p%04d.tiff','--resolution', '600', '--mode', 'Gray', '--batch-count=1']); scanimage.stdout.on('data', function (data) { console.log('stdout: ' + data); }); scanimage.stderr.on('data', function (data) { console.log('stderr: ' + data); }); scanimage.on('exit', function (code) { console.log('child process exited with code ' + code); }); } 

但是,现在扫描甚至不会启动,但输出的第一行似乎是正确的:

 Request handler 'scanner' was called. stderr: Scanning 1 pages, incrementing by 1, numbering from 1 child process exited with code null 

奇怪的是,“扫描1页,递增1,从1开始编号”现在来自stderr,并且过程在此之后退出。

任何想法如何debugging呢?