鍍金池/ 問(wèn)答/Linux  HTML/ child_process exec怎么不關(guān)閉進(jìn)程?

child_process exec怎么不關(guān)閉進(jìn)程?

express框架下的路由代碼

var express = require('express');
var exec = require('child_process').exec;
var router = express.Router();

router.post('/ssh', function(request, response, next) {
  exec(request.body.command, function(error, stdout, stderr){
    if(error) {
        console.error('error: ' + error);
        response.send(error);
    }
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + typeof stderr);
    response.send(stdout);
  });
})

我的命令是從前端傳入的請(qǐng)求參數(shù),但是每次輸入新的命令都會(huì)生成一個(gè)不同的進(jìn)程來(lái)執(zhí)行,當(dāng)我輸入第二個(gè)命令的時(shí)候,已經(jīng)不是處于第一個(gè)命令的進(jìn)程了.多個(gè)命令必須要用;隔開(kāi)才能完整執(zhí)行

有什么辦法解決嗎

回答
編輯回答
空痕

child_process.exec() 不會(huì)替換現(xiàn)有的進(jìn)程,且使用一個(gè) shell 來(lái)執(zhí)行命令

2017年7月27日 13:56