节点服务器在通过监控进行监控时停止



我正在尝试监视一个在节点服务器中运行的简单helloworld脚本。这个应用程序不用管就可以正常工作。但当我使用monit进行监控时,我可以看到节点服务器每次都会停止并被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/

monitrc

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'

monitrc

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

Monit可以处理Node脚本,但您应该在一个新的过程中启动Node脚本。

check host Helloworld with address localhost
start program "/bin/bash -c '/usr/local/bin/node /Users/lutz/node/helloworld.js >/dev/null 2>&1 &'" with timeout 60 seconds
stop program "/bin/bash -c 'pkill -TERM helloworld.js'"
if failed port 3000 with protocol http with timeout 15 seconds then restart

节点本身不会切换到新进程。