-2

好的,作为 node.js 的完整新手,我希望我听起来并不无知。我正在尝试编写一个非常简单的应用程序来通过发出 GET 来监视 REST 端点,并且假设它得到一个有效的响应,等待给定的时间然后再次运行。

我对请求没有任何问题,如果需要,可以使用 cron 作业运行它。但是,我想把它作为一个守护进程运行。有没有办法在节点中创建这样的服务器,可能有一个无限循环,只在错误和超时时退出?或者更好,更清洁的东西?

提前致谢。

4

1 回答 1

3

您可以setInterval在传递的闭包中使用并运行用于监控 REST 服务的代码:

setInterval(function () {
    // TODO: place the code for monitoring the REST service here
}, 60000); // change the interval to your liking.

您还可以使用一个名为forever的包来确保您的脚本连续运行。

为了将脚本作为守护进程运行,您可以(可选)添加节点 shebang ( #!/usr/bin/env node) 并编写一个init script来运行它:

#!/bin/sh

runOnBackground() {
    # You may want to save the output into a log file instead of redirecting it to /dev/null
    nohup $* > /dev/null 2>&1 &
}

# in your init script, when you want to start the script
runOnBackground /path/to/your/node/script.js
于 2013-04-30T21:21:42.237 回答