简体   繁体   English

如何在停止服务的地方检查服务状态

[英]How to check service status where service is stoped

I noted very weird behavior when I want to run service apache2 status on my node. 当我想在节点上运行服务apache2状态时,我注意到了非常奇怪的行为。 This is code: 这是代码:

 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);
   });

}); 

When I run this command when apache is running everything is ok, but when I stop apache and they I try to check status I get error: 当我在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)

What is wrong? 怎么了? Why when apache is running everything is ok? 为什么当Apache运行时一切正常?

You are checking for errors and throwing the error on this line: 您正在检查错误并在此行上抛出错误:

if(err) throw err;

So the execution stop and throw an error 所以执行停止并抛出错误

Do

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

As you can read in node docs : 正如您可以在node docs中阅读的那样:

If a callback function is provided, it is called with the arguments (error, stdout, stderr). 如果提供了回调函数,则会使用参数(错误,stdout,stderr)对其进行调用。 On success, error will be null. 成功后,错误将为null。 On error, error will be an instance of Error. 出现错误时,error将是Error的一个实例。 The error.code property will be the exit code of the child process while error.signal will be set to the signal that terminated the process. error.code属性将是子进程的退出代码,而error.signal将设置为终止进程的信号。 Any exit code other than 0 is considered to be an error. 除0以外的任何退出代码均被视为错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM