如何检查服务停止的服务状态

时间:2017-09-13 14:42:53

标签: node.js

当我想在节点上运行service apache2 status时,我注意到了非常奇怪的行为。这是代码:

 app.post('/getStatus', jsonParser, function(req,res){

   exec("service apache2 status", function(err, stdout){
    if(err) throw err;
    var statuspmtatmp = /Active: [a-z]* [\(][a-z]*[\)]/g.exec(stdout.toString())[0];

    res.json(statuspmtatmp);
   });

}); 

当我在运行apache时运行此命令一切正常,但是当我停止apache并且我尝试检查状态时出现错误:

Error: Command failed: /bin/sh -c service apache2 status
at ChildProcess.exithandler (child_process.js:213:12)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:821:16)
at Socket.<anonymous> (internal/child_process.js:319:11)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Pipe._onclose (net.js:469:12)

有什么问题?为什么当apache运行时一切正常?

1 个答案:

答案 0 :(得分:1)

您正在检查错误并在此行上抛出错误:

if(err) throw err;

所以执行停止并抛出错误

待办事项

if(err){
   //code to handle the error
}

您可以阅读node docs

  

如果提供了回调函数,则使用参数(error,stdout,stderr)调用它。成功时,错误将为空。 出错时,错误将是错误的实例。 error.code属性将是子进程的退出代码,而error.signal将设置为终止进程的信号。 除0以外的任何退出代码都被视为错误。

相关问题