Forever example of continuous running of a node server

Source: Internet
Author: User
Tags app service node server

When you use node to set up a server, when we close the service window, node breaks down, and then, how to keep the service there, node has a good module to do the job, forever.

Here are some things to note about the Forever module:

Using forever to run NODEJS applications
What Is forever
Forever can be seen as a Nodejs daemon that can start, stop, and restart our app apps.

The official note is that:
A simple CLI tool to ensuring that a given script runs continuously (i.e. forever).
A simple command-line tool for continuing (or forever) running a given script

GitHub Address: Https://github.com/nodejitsu/forever
Use

The purpose of forever is to help us better manage our Node app service, essentially creating a node app's subprocess under the forever process.

For example, if you have a express based or some other application, it will be very convenient for you to update and operate your service, and ensure that your service continues to operate.

The better thing is that every time you change a file, it automatically restarts the service without having to restart it manually.
Install Forever

Remember to add-g,forever requirements to install to the global environment
sudo npm install forever-g

Forever Use Instructions
Start related

1. Easy to start
Forever Start App.js

2. Specify forever information output file, of course, by default it will be placed in ~/.forever/forever.log
Forever Start-l Forever.log App.js

3. Specifies the log information and error log output files in App.js.
-O is the Console.log output information, E is the Console.error output information
Forever Start-o out.log-e Err.log app.js

4. Append log, forever default is unable to overwrite the last boot log,
So if the second boot does not add-a, it will not let the running
Forever Start-l forever.log-a App.js

5. Listen for all file changes under the current folder
Forever Start-w App.js

File changes are monitored and automatically restarted

1. Listens for all file changes under the current folder (this is not recommended)
Forever Start-w App.js

Show all running Services

Forever List

Stop operation

1. Stop all the running node apps
Forever StopAll

2. Stop one of the node apps
Forever Stop App.js
Of course I can.
The Forever list finds the corresponding ID, and then:
Forever Stop [ID]

Restart operation

The restart operation is consistent with the stop operation.

1. Start all
Forever Restartall

Some more

Some of the above explanations are common enough and need to be supplemented later.
Development and online recommended configuration

Under development environment
Node_env=development Forever start-l forever.log-e err.log-a app.js
In the online environment
Node_env=production Forever start-l ~/.forever/forever.log-e ~/.forever/err.log-w-a app.js

Add node_env to allow App.js to identify what the current environment is. Without it, you might not know it.
Some attention.

It is possible that you need to use the crontab (timed task) under Unix

This time you need to pay attention to configuring environment variables.

Shell=/bin/sh
Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.