通过 monit 监控时节点服务器停止

问题描述

我正在尝试监视在节点服务器中运行的简单 helloworld 脚本。该应用程序可以正常工作。但是我用monit监控的时候,可以看到node服务器停止了,每次都被monit反复重启。

有人可以帮我吗?

hello-world.js

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req,res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type','text/plain');
      res.end('Hello,World!\n');
});

server.listen(port,hostname,() => {
      console.log(`Server running at http://${hostname}:${port}/`);
});

monit.log

WIB Jun  9 14:51:07] error    : 'selva-dev1' Failed protocol test [HTTP] at [localhost]:3000 [TCP/IP] -- Connection refused
[WIB Jun  9 14:51:07] info     : 'selva-dev1' trying to restart
[WIB Jun  9 14:51:07] info     : 'selva-dev1' start: '/usr/bin/node /home/selva/tools/testnode/hello-world.js'


[WIB Jun  9 14:52:07] error    : 'selva-dev1' Failed to start (exit status -1) -- '/usr/bin/node /home/selva/tools/testnode/hello-world.js': Program timed out -- Server running at http://127.0.0.1:3000/

[WIB Jun  9 14:52:22] error    : 'selva-dev1' Failed protocol test [HTTP] at [localhost]:3000 [TCP/IP] -- Connection refused
[WIB Jun  9 14:52:22] info     : 'selva-dev1' trying to restart
[WIB Jun  9 14:52:22] info     : 'selva-dev1' start: '/usr/bin/node /home/selva/tools/testnode/hello-world.js'
[WIB Jun  9 14:53:23] error    : 'selva-dev1' Failed to start (exit status -1) -- '/usr/bin/node /home/selva/tools/testnode/hello-world.js': Program timed out -- Server running at http://127.0.0.1:3000/

[WIB Jun  9 14:53:38] error    : 'selva-dev1' Failed protocol test [HTTP] at [localhost]:3000 [TCP/IP] -- Connection refused
[WIB Jun  9 14:53:38] info     : 'selva-dev1' trying to restart
[WIB Jun  9 14:53:38] info     : 'selva-dev1' start: '/usr/bin/node /home/selva/tools/testnode/hello-world.js'
[WIB Jun  9 14:54:38] error    : 'selva-dev1' Failed to start (exit status -1) -- '/usr/bin/node /home/selva/tools/testnode/hello-world.js': Program timed out -- Server running at http://127.0.0.1:3000/

监控

check host selva-dev1 with address localhost
start program = "/usr/bin/node /home/selva/tools/testnode/hello-world.js " with timeout 60 seconds
if Failed port 3000 with protocol http with timeout 15 seconds then restart

更新

根据 siebteman 和 marc 的建议,我尝试了另一个不涉及主机名解析的脚本

大写.js

var http = require('http');
var uc = require('upper-case');
http.createServer(function (req,res) {
      res.writeHead(200,{'Content-Type': 'text/html'});
      /*Use our upper-case module to upper case a string:*/
      res.write(uc.upperCase("Hello World!"));
      res.end();
}).listen(8080);

Node 服务器仍然崩溃。这就是为什么 monit 每次都试图重新启动它。

[WIB Jun  9 15:53:54] error    : 'selva-dev1' Failed to start (exit status -1) -- Program '/usr/bin/node /home/selva/tools/testnode/uppercase.js' timed out after 1 m
[WIB Jun  9 15:54:09] error    : 'selva-dev1' Failed protocol test [HTTP] at [selva-dev]:8080 [TCP/IP] -- Connection refused
[WIB Jun  9 15:54:09] info     : 'selva-dev1' trying to restart
[WIB Jun  9 15:54:09] info     : 'selva-dev1' start: '/usr/bin/node /home/selva/tools/testnode/uppercase.js'
[WIB Jun  9 15:55:09] error    : 'selva-dev1' Failed to start (exit status -1) -- Program '/usr/bin/node /home/selva/tools/testnode/uppercase.js' timed out after 1 m
[WIB Jun  9 15:55:24] error    : 'selva-dev1' Failed protocol test [HTTP] at [selva-dev]:8080 [TCP/IP] -- Connection refused
[WIB Jun  9 15:55:24] info     : 'selva-dev1' trying to restart
[WIB Jun  9 15:55:24] info     : 'selva-dev1' start: '/usr/bin/node /home/selva/tools/testnode/uppercase.js'
[WIB Jun  9 15:56:24] error    : 'selva-dev1' Failed to start (exit status -1) -- Program '/usr/bin/node /home/selva/tools/testnode/uppercase.js' timed out after 1 m
[WIB Jun  9 15:56:39] error    : 'selva-dev1' Failed protocol test [HTTP] at [selva-dev]:8080 [TCP/IP] -- Connection refused
[WIB Jun  9 15:56:39] info     : 'selva-dev1' trying to restart
[WIB Jun  9 15:56:39] info     : 'selva-dev1' start: '/usr/bin/node /home/selva/tools/testnode/uppercase.js'

监控

check host selva-dev1 with address selva-dev
start program = "/usr/bin/node /home/selva/tools/testnode/uppercase.js"  with timeout 60 seconds
if Failed port 8080 with protocol http with timeout 15 seconds then restart

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)