2013-04-30 71 views
-2

好的,作为node.js的一个完整的新手,我很抱歉,我听不出无知。我正在尝试编写一个非常简单的应用程序来通过发出GET来监视REST端点,并假设它获得有效的响应,则等待一段时间,然后再次运行。Node.js服务?

我对这些请求没有任何问题,并且可以在需要时使用cron作业运行此操作。不过,我想只将它作为守护进程来运行。有没有一种方法可以在节点上创建一个像这样的服务器,可能会出现一个只能在出错和超时时退出的无限循环?或者更好,更干净的东西?

在此先感谢。

回答

3

您可以使用setInterval和运行代码在通过关闭监控REST服务:

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

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

为了运行脚本作为一个守护进程,你可以(可选)添加节点家当(#!/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 
+0

正是我需要的!谢谢! – RockyMountainHigh 2013-04-30 21:26:57

+1

@RockyMountainHigh欢迎您。看看我更新的答案。 – fardjad 2013-04-30 21:40:19

+0

哇!有趣的是,这是“不是真正的问题”,因为@fardjad在短时间内没有问题回答。 – RockyMountainHigh 2013-05-01 16:37:32