node.js - 节点服务器在通过 monit 监控时停止
问题描述
我正在尝试监视在节点服务器中运行的简单 helloworld 脚本。该应用程序可以正常工作。但是当我用monit监控的时候,可以看到node server停止了,每次都被monit反复重启。
有人可以帮帮我吗?
你好世界.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}/`);
});
监控日志
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);
仍然节点服务器不断崩溃。这就是为什么 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