Manage the nodejs application tutorial using forever
- 2020-03-30 03:08:13
- OfStack
What is forever
Forever can be seen as a nodejs daemon that can start, stop, and restart our app.
The official statement said:
A simple CLI tool for ensuring that a given script runs continuously (i.e. forever).
// A simple command-line tool for running a given script continuously (or forever)
Making address: https://github.com/nodejitsu/forever
Forever purpose
Forever's purpose is to help us better manage our node App service, essentially creating a child of a node App under the forever process.
For example, if you have an application based on express or some other application, it will make it easy for you to update and operate your service and keep it running.
Even better, every time you change a file, it automatically restarts the service without having to restart it manually.
The installation of forever
//Remember to add -g, forever to the global environment
sudo npm install forever -g
Instructions for forever
Start the related
//1. Simple startup
forever start app.js
//2. Specify the forever message output file, which, of course, will be placed in ~/.forever/forever.log by default
forever start -l forever.log app.js
//3. Specify the log information and error log output file in app.js,
// -o is the output information of console.log, and -e is the output information of console.error
forever start -o out.log -e err.log app.js
//4. Append log, forever by default cannot overwrite the last startup log,
// So if you start the second time without minus a, you won't let it run
forever start -l forever.log -a app.js
//5. Listen for all file changes in the current folder
forever start -w app.js
File changes are monitored and automatically restarted
//1. Listen for all file changes in the current folder (not recommended)
forever start -w app.js
Displays all running services
forever list
Stop the operation
//1. Stop all running node apps
forever stopall
//2. Stop one of the node apps
forever stop app.js
//Of course you can
//Forever list finds the corresponding id and then:
forever stop [id]
Restart the operating
The restart operation is the same as the stop operation.
//1. Start all
forever restartall
Development and online recommended configuration
//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
I'm going to add NODE_ENV up here in order for app.js to know what environment it's in. You might not know without it, right?
Some attention points
You may need to use a Unix crontab (timing task)
At this point, you need to be careful to configure the environment variables.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin