2016-04-20 242 views
2

我有一个节点应用程序,我使用bitbucket中的webhook来触发bash脚本来运行一些命令来部署我的应用程序。它工作,但我不能查看任何正在运行的命令(即进度命令echo'd)。我怎样才能做到这一点?如何从节点脚本执行时查看bash命令

下面是相关代码:

var shellPath = path.join(__dirname + '../../deploy/deploy.sh'); 
    var exec = require('child_process').exec; 

    function puts(error, stdout, stderr) { 
    console.log('stout: ', stdout) 
    console.log('error: ', error) 
    console.log('stderr: ', stderr) 
    } 

    exec(shellPath, puts); 

deploy.sh -

echo Preparing to Git Fetch 
git reset --hard origin/master 
echo Preparing to clean 
git clean -f 
echo Preparing to pull 
git pull 
echo preparing to pull 
npm i --production 
echo preparing to restart pm2 
pm2 restart client-www 
echo Finished deploy 
+1

请参见:[如何调试bash脚本?](http://unix.stackexchange.com/q/155551/74329) – Cyrus

回答

0

你可以只将结果输出到像一个文件:

var shellPath = path.join(__dirname + '../../deploy/deploy.sh > ./output.log'); 
var exec = require('child_process').exec; 

function puts(error, stdout, stderr) { 
    console.log('stout: ', stdout) 
    console.log('error: ', error) 
    console.log('stderr: ', stderr) 
} 

exec(shellPath, puts); 

,并以此来手表文件和打印输出always-tail

var Tail = require('always-tail'); 
var fs = require('fs'); 
var filename = "./output.log"; 

if (!fs.existsSync(filename)) fs.writeFileSync(filename, ""); 

var tail = new Tail(filename, '\n'); 

tail.on('line', function(data) { 
    console.log("got line:", data); 
}); 


tail.on('error', function(data) { 
    console.log("error:", data); 
}); 

tail.watch();