What is forever
Forever can be regarded as a nodejs daemon that can start, stop and restart our app application.
The official explanation says:
The code copy is as follows: A simple CLI tool for ensuring that a given script runs continuously (ie forever).
// A simple command line tool for running a given script continuously (or forever)
Github address: https://github.com/nodejitsu/forever
forever uses
The purpose of forever is to help us better manage our node App services. In essence, it is to create a child process of node app under the forever process.
For example, if you have an express or some other application, it will be very convenient for you to update and operate your services and ensure that your services can continue to run.
A better point is that every time you change the file, it can help you automatically restart the service without manually restarting it.
Install forever
The code copy is as follows:
// Remember to add -g, forever requires installation to be in the global environment
sudo npm install forever -g
Forever Instructions
Start-up related
The code copy is as follows:
// 1. Simple start
forever start app.js
// 2. Specify the forever information output file. Of course, by default it will be placed in ~/.forever/forever.log
forever start -l forever.log app.js
// 3. Specify the log information and error log output file in app.js.
// -o is the information output from console.log, and -e is the information output from console.error
forever start -o out.log -e err.log app.js
// 4. Append logs. Forever cannot overwrite the last startup log by default.
// So if the second startup does not add -a, it will not be allowed to run
forever start -l forever.log -a app.js
// 5. Listen to all file changes in the current folder
forever start -w app.js
File changes and automatically restart
The code copy is as follows:
// 1. Listen to all file changes in the current folder (not recommended)
forever start -w app.js
Show all running services
Copy the code as follows: forever list
Stop the operation
Copy the code as follows:// 1. Stop all running node apps
forever stopall
// 2. Stop one of the node apps
forever stop app.js
// Of course it can be
// forever list Find the corresponding id, and then:
forever stop [id]
Restart operation
The restart operation is consistent with the stop operation.
Copy the code as follows:// 1. Start all
forever restartall
Development and online configuration
The code copy is as follows:
// In the 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
NODE_ENV is added above to let app.js identify what environment it is currently used. Maybe you don't know if you don't add it?
Some notes
Maybe you need to use crontab under unix (timed tasks)
At this time, you need to pay attention to configuring environment variables.
Copy the code as follows: SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin