Keep A Node.Js Server Up With Forever

 

把你的nodejs脚本交给forever吧,它可以让你的nodejs起死回生!

 

文章来源:http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever

 

One of the great benefits of using node.js is the reduction in dependencies needed to run a production web application. By using the 'http' module we can run a stand-alone web server in node.js without the need for a separate server like Apache or nginx. The caveat of not having to use these servers is that their concerns are now the concerns of the node.js application developer. The concern that we will discuss in this article is that of fault tolerance, or how to automatically restart your server when it crashes or enters an invalid state.

Before we dive into how to use Forever, lets setup the 'hello world' web server that we will later run with Forever.

simple-server.js
 

The above code starts a web server that will respond to all requests with 'hello, i know nodejitsu'. Starting this server is easy:

 

Running in the background

By simply running our server script with the 'node' command the server will start as a long running process (i.e. it will block the current shell until it crashes or is forced to exit with Ctrl-C). But what if we want to run this server in the background? The traditional approach is to run the command using nohup:

 

This will start our server process in the background and append all output to 'output.log'. Another approach is to start the script as a daemon using a library like daemon.node. I won't go into the details of using daemon.node, but the documentation on the Github project page. These two approaches solve the background problem, but what if we have a buggy server like the one below:

buggy-server.js
 

This is a contrived example that will start a web server, then throw an unhandled exception after five seconds. If we were to run this script, either in the background or a long running process, it will not automatically restart when it crashes.

Keeping it running with Forever

The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly. It's worth mentioning that there are other tools written to accomplish this task in a more generic way for any program or programming language:

  1. Monit: http://mmonit.com/monit/
  2. Upstart: http://upstart.ubuntu.com/
  3. Daemontools: http://cr.yp.to/daemontools.html
  4. Launchtool: http://people.debian.org/~enrico/launchtool.html


Currently Forever is for running node.js scripts only, but this is something that may be changed in the future. Honestly, it's a one line fix here, but I'm not sure if users want to put 'node' in-front of every command. Forever is used in production at Nodejitsu and we can confirm that since we deployed it on our servers a month ago, we have not needed to touch it at all. Forever exposes its functionality via a command line interface available after installation via npm:

 

The usage options for Forever expose four simple command line tasks: start, stop, stopall, list:

 

You can also use Forever as a long running process by omitting the 'start' option. With these tasks available, starting a nodejs script with Forever is simple:

 

The first command starts the 'simple-server.js' script in the background using daemon.node and returns control to the current shell process. The second command lists all processes running with Forever. The IDs after the script name are the process IDs of the target script and the forever daemon watching that script respectively. We can confirm this by looking at the process list ourselves:

 

Stopping and restarting with Forever

To illustrate that Forever will automatically restart a child process that exits, lets kill the process ourselves:

 

As you can see the process ID of the target script 'simple-server.js' has changed from 24597 to 24611 indicating that a new process has been started by Forever. So our target script will run continuously, but how do we stop it? Stopping a process with Forever is simple from the command line. We simply need to pass the index for that process show from 'forever list':

 

Additional Forever options

There are some default options and configuration conventions that you should be aware of when using Forever:

  1. Forever keeps track of running processes in *.fvr files that are placed in /tmp/forever/pids
  2. Each Forever process will generate a unique log file placed in /tmp/forever/*.log
  3. Unless otherwise specified, the output of the child process' stdout and stderr will be written to the above log file.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值