2017-08-26 62 views
2

我正在使用此節點的js代碼來運行包含無限循環的App.exe文件。我通過輸入並從App.exe獲取輸出。使用節點j關閉.exe文件

var bat = null; 
app.post("/api/run", function(req, res) { 
    if(req.body.success) { 
     if(bat) bat.kill(); 
    } 
    if(!bat) { 
     bat = spawn('cmd.exe', ['/c App.exe']); 
     bat.stderr.on('data', function (data) { 
      res.end(JSON.stringify({error: true, message: data.toString()})); 
     }); 

     bat.on('exit', function (code) { 
      bat = null; 
      console.log('Child exited with code ' + code); 
      res.end(JSON.stringify({error: false, message: "Application Closed!"})); 
     }); 
    } 
    bat.stdin.write(req.body.input+'\n'); 
    bat.stdout.once('data', function (data) { 
     console.log(data.toString()); 
     res.end(JSON.stringify({error: false, message: data.toString()})); 
    }); 
}); 

問題是,當我殺死子進程時,子進程被殺死,但App.exe繼續運行。我有沒有辦法阻止App.exe運行

+1

代替'cmd.exe',你不能直接啓動App.exe嗎? – robertklep

回答

2

找到相反產卵cmd.exe其產卵App.exe的東西,你可以直接產卵後:

bat = spawn('App.exe'); 
0

爲了殺死node.js中的衍生進程,您需要使用SIGINT

bat.kill('SIGINT'); 

所有的POSIX信號列表和他們做可以在signal7

+1

我試過了,它沒有工作。它殺死了子進程,但app.exe仍在運行。感謝您的回覆 –