NodeJs process daemon forever

Source: Internet
Author: User

Nodejs is a single process. If an uncaptured exception occurs in the application, the process Exits. A stupid way is to use a scheduled task in the system to check whether the process exists, if the program is not started, this method will waste system resources.

Fortunately, the active node. Js community provides developers with a "forever" option, which is used to solve the above situation.

Use

It is easy to use. There are only two commands.

Npm install forever-g
Forever start app. js

You can run the application. If the application fails, it will automatically restart.

View the list of monitored processes

Forever list

Manage nodejs using forever

Forever instructions

Startup problems

// 1. Simple startup
Forever start app. js

// 2. Specify the forever information output file. Of course, it will be placed in ~ by default ~ /. Forever/forever. log
Forever start-l forever. log app. js

// 3. Specify the log information and error log output file in app. js,
//-O indicates the information output by console. log, and-e indicates the information output by console. error.
Forever start-o out. log-e err. log app. js

// 4. Append logs. By default, forever cannot overwrite the last startup log,
// If-a is not added for the second start, the operation will not be allowed
Forever start-l forever. log-a app. js

// 5. Listen for all file changes in the current folder
Forever start-w app. js

Monitors file changes and restarts automatically

// 1. Listen for all file changes in the current folder (this is not recommended)
Forever start-w app. js
Show all running services

Forever list
Stop operation

// 1. Stop all running node apps
Forever stopall

// 2. Stop one of the node apps
Forever stop app. js
// Of course you can do this.
// Forever list find the corresponding id, and then:
Forever stop [id]

Restart operation

The restart operation is consistent with the stop operation.
// 1. Start all
Forever restartall
Recommended Configurations for development and production

 

// In the development environment
NODE_ENV = development forever start-l forever. log-e err. log-a app. js
// Online environment
NODE_ENV = production forever start-l ~ /. Forever/forever. log-e ~ /. Forever/err. log-w-a app. js
Add NODE_ENV above to Let app. js identify the current environment. Maybe I don't know if I don't add it?

Notes

You may need to use crontab in unix (scheduled task)
In this case, you must configure 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.