2016-04-20 287 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();