What is forever?
Forever can be seen as a nodejs daemon that can start, stop, and restart our app.
The official description is as follows:
Copy codeThe Code is as follows: A simple CLI tool for ensuring that a given script runs continuously (I. e. forever ).
// A simple command line tool used to continuously (or permanently) run a given script
Github address: https://github.com/nodejitsu/forever
Forever Purpose
The purpose of forever is to help us better manage our node App services. In essence, it is to create a node app sub-process under the forever process.
For example, if you have an express-based or other application, it will facilitate you to update and operate your service and ensure that your service can continue to run.
The better thing is that every time you change the file, it can automatically restart the service without manual restart.
Install forever
Copy codeThe Code is as follows:
// Remember to add-g and forever to the global environment.
Sudo npm install forever-g
Forever instructions
Startup Problems
Copy codeThe Code is as follows:
// 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
Copy codeThe Code is as follows:
// 1. Listen for all file changes in the current folder (this is not recommended)
Forever start-w app. js
Show all running services
Copy codeThe Code is as follows: forever list
Stop operation
Copy codeThe Code is as follows: // 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.
Copy codeThe Code is as follows: // 1. Start all
Forever restartall
Recommended configurations for Development and Production
Copy codeThe Code is as follows:
// 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.
Copy codeThe Code is as follows: SHELL =/bin/sh
PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin