Running Node.js Forever

If we use node to run Node.js script, it will block the current shell until it crashes or is forced to exit with Ctrl-C. But sometimes we might need to run this service in background or manage the Node.js

The traditional way is to use nohup.

$ nohup node hello.js > output.log &

Or you can choose some libraries such as daemon.node. So, if we already has the way to run service in background, what’s the benefit of using Forever?

Forever is not just for running script in background, it can automatically restart the service when it stop unexpectedly.

Let’s install Forever and check it’s help document.

$ sudo npm install forever
$ forever --help

If you want to run your script in background, just use forever start instead of node.

$ forever start hello.js
Info: Forever processing file: hello.js

When you need to stope the running process, you can use forever list to see all your running process.

$ forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] mn0M /usr/local/bin/node hellp.js 86298 86299 /Users/evahsieh/.forever/mn0M.log 0:0:0:9.207
$ forever stop 0

Related Articles:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s