nodejs在linux中运行shell脚本时会发出不需要的消息

时间:2017-03-22 12:00:38

标签: node.js bash shell rest express

尝试使用nodejs执行shell脚本,而nodejs服务器提供错误的错误消息

shell脚本

sudo mkdir updateinprogress
servicebranch="Development"
currentpath="$PWD"
tarfilename="$(date +'%d-%m-%Y_%H-%M-%S')"
cd ..
if [ ! -d "backups" ]; then
    sudo mkdir backups
fi
cd backups
if [ ! -d "AppInEngine" ]; then
    sudo mkdir AppInEngine
fi
cd $currentpath
if [ ! -d "current_file_backup" ]; then
    sudo mkdir current_file_backup
fi
sudo cp -r /var/www/AppInEngine $currentpath/current_file_backup/
sudo git clone https://github.com/abc/AppInEngine --branch $servicebranch -q
if [ $? != 0 ]; then
    echo -e "{\"status\":\"false\", \"message\":\""error occerd while cloning git repository "\"}"
    sudo rm -r AppInEngine updateinprogress current_file_backup
    exit
fi
sudo cp  -r /var/www/AppInEngine/configs AppInEngine
sudo cp -r /var/www/AppInEngine/Logs AppInEngine/
sudo rm -r /var/www/AppInEngine
sudo cp -r AppInEngine /var/www
sudo chmod -R 777 /var/www/AppInEngine
service apache2 restart > /dev/null
if [ $? != 0 ]; then
    sudo rm -r /var/www/AppInEngine
    sudo cp -r $currentpath/current_file_backup/AppInEngine /var/www/
    sudo service apache2 restart > /dev/null
    sudo rm -r AppInEngine updateinprogress current_file_backup
    echo -e "{\"status\":\"false\", \"message\":\""error occerd while starting apache2 .restart with backup "\"}"
    exit
fi
sleep 160 

if curl -s --head  --request GET http://localhost/AppInEngine/get_version | grep "200 OK" > /dev/null; then 
  sudo tar -cf AppInEngine_$tarfilename.tar.gz current_file_backup/AppInEngine
  sudo mv AppInEngine_$tarfilename.tar.gz ../backups/AppInEngine/
  cd ../backups/AppInEngine
  #sudo cp AppInEngine$tarfilename.tar.gz 
  sudo rm -f $(ls -1t | tail -n +3)
  cd $currentpath
  sudo rm -r AppInEngine updateinprogress current_file_backup
  echo -e "{\"status\":\"true\", \"message\":\""service update success"\"}"
  exit
else
  echo -e "{\"status\":\"false\", \"message\":\""service update faild .trying to start with backup  "\"}"
  sudo service apache2 stop
  sudo rm -r /var/www/AppInEngine
  sudo cp -r $currentpath/current_file_backup/AppInEngine /var/www/
  sudo rm -r AppInEngine ls  current_file_backup
  chmod -R 777 /var/www/AppInEngine
  sudo service apache2 start  > /dev/null
  exit
fi

nodejs服务器

    var express = require('express');
var app = express();
var exec = require('child_process').exec, child;
app.listen(4105);
app.get('/updateEngin',function (req, res){
    var testscript = exec('sh test.sh /var/www/update/serviceupdate');
    testscript.stdout.on('data', function(data){
        console.log(data); 
        res.json(data);  
    });

});
在终端(./test.sh)中单独运行脚本时输出

{“status”:“true”,“message”:“服务更新成功”}

使用nodejs

运行时的输出

{“status”:“false”,“message”:“克隆git存储库时出现错误”} {“status”:“true”,“message”:“服务更新成功”}

1 个答案:

答案 0 :(得分:0)

将时间设置为nodejs服务器 问题将得到解决

var express = require('express');
var app = express();
var timeout = require('connect-timeout');
var exec = require('child_process').exec, child;
app.listen(4105);
app.get('/updateEngin', timeout('160s'), function (req, res){
        var testscript = exec('sh test.sh /var/www/update/serviceupdate');
        testscript.stdout.on('data', function(data){
        console.log(data);
        res.json(data);
        });

});