Next, let's see if forever can achieve the goal.
First, Forever Introduction
Run node js forever
Forever is a simple command-nodejs daemon that can start, stop, and restart app apps. Forever is completely based on command line operation, under the Forever process, create node subprocess, monitor the operation of node subprocess through monitor, once the file is updated, or the process hangs, forever will automatically restart the node server to ensure the application is running properly.
Node forever
Second, forever installation
How to run node server forever
Global Install Forever
Copy Code code as follows: forever monitor node js
~ D:\WORKSPACE\JAVASCRIPT>NPM Install-g Forever
D:\toolkit\nodejs\forever-> D:\toolkit\nodejs\node_modules\forever\bin\forever
D:\toolkit\nodejs\foreverd-> D:\toolkit\nodejs\node_modules\forever\bin\foreverd
View Forever Help
Copy Code code as follows:
~ d:\workspace\javascript>forever-h
Help:usage:forever [Action] [options] SCRIPT [Script-options]
Help
Help:monitors the script specified in the current process or as a daemon
Help
Help:actions:
Help:start start SCRIPT as a daemon
Help:stop Stop the daemon SCRIPT
Help:stopall Stop all running forever scripts
Help:restart Restart the daemon SCRIPT
Help:restartall Restart all running forever scripts
Help:list List all running forever scripts
Help:config Lists All Forever User Configuration
Help:set <key> <val> Sets The specified forever config <key>
Help:clear <key> Clears the specified forever config <key>
Help:logs Lists log files for all forever processes
Help:logs <script|index> Tails the logs for <script|index>
Help:columns Add <col> Adds The specified column to the output in ' Forever list '
Help:columns RM <col> removed the specified column from the output in ' Forever list '
Help:columns set <cols> set all columns to the output in ' Forever list '
Help:cleanlogs [careful] deletes all historical forever log files
Help
Help:options:
Help:-M Max only run the specified script max times
Help:-L LOGFILE Logs the forever output to LOGFILE
Help:-o outfile Logs stdout from child script to outfile
Help:-E errfile Logs stderr from child script to Errfile
Help:-P path Base PATH for all Forever related files (PID files, etc.)
Help:-C command command to execute (defaults to node)
Help:-A,--append append logs
Help:-F,--fifo Stream logs to stdout
Help:-N,--number number of log lines to print
Help:--pidfile the PID file
Help:--sourcedir The source directory for which SCRIPT is relative to
Help:--minuptime Minimum uptime (Millis) for a script to not is considered "spinning"
Help:--spinsleeptime time to wait (Millis) between launches of a spinning script.
Help:--colors--no-colors'll disable output coloring
Help:--plain alias of--no-colors
Help:-D,--debug forces forever to log debug output
Help:-V,--verbose turns on the verbose messages from Forever
Help:-S,--silent Run the child script silencing stdout and stderr
Help:-W,--watch watch for file changes
Help:--watchdirectory top-level directory to watch from
Help:--watchignore to ignore pattern when watch are enabled (multiple option is allowed)
Help:-H,--help ' re staring at it
Help
Help: [Long Running Process]
Help:the forever process would continue to run outputting log messages to the console.
Help:ex. Forever-o out.log-e Err.log my-script.js
Help
Help: [Daemon]
Help:the forever process would run as a daemon which'll make the target process start
Help:in the background. This is extremely useful to remote starting simple node.js scripts
Help:without using Nohup. It is recommended to run start With-o-L, &-E.
Help:ex. Forever start-l forever.log-o out.log-e err.log my-daemon.js
Help:forever Stop My-daemon.js
Help
We see that forever supports a number of commands and configuration items, which should be a management tool on the partial command line.
Third, the Forever command line in Chinese interpretation
Child Command actions:
Copy Code code as follows:
Start: Starting the daemon
Stop: Stopping the daemon
StopAll: Stop All Forever processes
Restart: Reboot daemon
Restartall: Reboot all Foever processes
List: Listing shows forever process
Config: Lists all user Configuration items
Set <key> <val>: Setting User Configuration Items
Clear <key>: Clearly User Configuration items
Logs: Lists the logs for all forever processes
Logs <script|index>: Display the latest log
Columns add <col>: Customizing metrics to Forever List
Columns rm <col>: Delete Forever list metrics
Columns Set<cols>: Set all metrics to Forever list
Cleanlogs: Delete all Forever history logs
Configuration Parameters Options:
Copy Code code as follows:
-M MAX: number of times the specified script was run
-L LOGFILE: output log to LOGFILE
-o outfile: output console information to outfile
-E errfile: Output console Error in Errfile
-P PATH: root directory
-C command: Execute commands, default is node
-a,–append: Merge Logs
-f,–fifo: Streaming Log output
-n,–number: Number of log print lines
–pidfile:pid file
–sourcedir: Source code Directory
–minuptime: Minimum spinn update time (MS)
–spinsleeptime: Two times spin time interval
–colors: Console Output coloring
–plain:–no-colors alias, console output colorless
-d,–debug:debug mode
-v,–verbose: Print verbose output
-s,–silent: Do not print logs and error messages
-w,–watch: Monitoring file changes
–watchdirectory: Monitoring the top-level directory
–watchignore: Ignoring monitoring through pattern matching
-h,–help: Command line help information
Iv. Forever Server Management
Create a Web project (EXPRESS3+EJS) and use forever to manage the server.
Install EXPRESS3
Copy Code code as follows:
~ D:\workspace\javascript>express-e Nodejs-forever
~ D:\WORKSPACE\JAVASCRIPT>CD nodejs-forever && npm Install
Start the application via forever
Copy Code code as follows:
~ D:\workspace\javascript\nodejs-forever>forever Start App.js
Warn:--minuptime not set. Defaulting to:1000ms
Warn:--spinsleeptime not set. Your script would exit if it does not stay up to least 1000ms
Info:forever processing File:app.js
Open Browser: http://localhost:3000, you can see the Web interface
View forever status under Win
Copy Code code as follows:
~ D:\workspace\javascript\nodejs-forever>forever List
Info:no Forever Processes Running
~ D:\workspace\javascript\nodejs-forever>forever Stop App.js
Error:forever cannot find process with Index:app.js
We found the Forever program, the work is wrong!! Program is clearly running state, through the list does not see. Next, switch to Linux Ubuntu to continue testing.
V. Forever Server Management in Ubuntu
Linux's system environment
Linux:ubuntu 12.04.2 64bit Server
node:v0.11.2
npm:1.2.21
Initialize Project: Installation command does not explain
Copy Code code as follows:
~ Cd/home/conan/nodejs
~ Express-e Nodejs-forever
~ CD nodejs-forever && npm Install
~ sudo npm Install forever-g
Start Forever
Copy Code code as follows:
~ Forever Start App.js
Warn:--minuptime not set. Defaulting to:1000ms
Warn:--spinsleeptime not set. Your script would exit if it does not stay up to least 1000ms
Info:forever processing File:app.js
View Node Server Status
Copy Code code as follows:
~ Forever List
Info:forever processes Running
Data:uid command Script forever pid logfile Uptime
Data: [0] l2ty/usr/local/bin/node app.js 18276 18279/home/conan/.forever/l2ty.log 0:0:0:37.792
# system Process
~ Ps-aux|grep Node
Warning:bad PS syntax, perhaps a bogus '-'? Http://procps.sf.net/faq.html
Conan 18296 0.5 1.1 597696 23776? SSL 15:48 0:00/usr/local/bin/node/usr/local/lib/node_modules/forever/bin/monitor App.js
Conan 18299 0.4 0.8 630340 18392? Sl 15:48 0:00/usr/local/bin/node/home/conan/nodejs/nodejs-forever/app.js
# port occupancy
~ Netstat-nltp|grep Node
TCP 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 18299/node
Stop server
Copy Code code as follows:
~ Forever Stop App.js
Info:forever stopped process:
Data:uid command Script forever pid logfile Uptime
[0] L2ty/usr/local/bin/node app.js 18276 18279/home/conan/.forever/l2ty.log 0:0:0:45.621
We see it as normal in a Linux Ubuntu environment.
VI. Simulate server downtime
Two kinds of test scenarios:
1. Use Linux command to kill node process directly
2. In application, simulate abnormal exit
1. Use Linux command to kill node process directly
Copy Code code as follows:
# view node process, pid=18299
~ Ps-aux|grep Node
Conan 18296 0.0 1.1 597696 23776? SSL 15:48 0:00/usr/local/bin/node/usr/local/lib/node_modules/forever/bin/monitor App.js
Conan 18299 0.0 0.8 630340 18392? Sl 15:48 0:00/usr/local/bin/node/home/conan/nodejs/nodejs-forever/app.js
Conan 18315 0.0 0.0 13584 956 pts/5 r+ 15:52 0:00 grep--color=auto node
# Kill pid=19299
~ kill-9 18299
# and look at the node process, node automatically restart, the new pid=18324
~ Ps-aux|grep Node
Conan 18296 0.0 1.1 597696 23916? SSL 15:48 0:00/usr/local/bin/node/usr/local/lib/node_modules/forever/bin/monitor App.js
Conan 18316 2.6 0.8 630340 18412? Sl 15:52 0:00/usr/local/bin/node/home/conan/nodejs/nodejs-forever/app.js
Conan 18324 0.0 0.0 13584 956 pts/5 r+ 15:52 0:00 grep--color=auto node
We see the Kill node process, forever will help us, restart node.
Kill Forever's Monitor.
Copy Code code as follows:
~ kill-9 18296
~ Ps-aux|grep Node
Conan 18316 0.0 0.9 630340 18644? Sl 15:52 0:00/usr/local/bin/node/home/conan/nodejs/nodejs-forever/app.js
Conan 18333 0.0 0.0 13584 952 PTS/5 s+ 15:57 0:00 grep--color=auto node
# and kill the node process
~ kill-9 18316
~ Ps-aux|grep Node
Conan 18336 0.0 0.0 13584 956 PTS/5 s+ 15:58 0:00 grep--color=auto node
We tried to kill the Forever Monitor,monitor program does not automatically reboot, and then kill the node process, node will not automatically restart.
2). In the application, simulate abnormal exit
Modify File: App.js
Copy Code code as follows:
~ VI app.js
//..
Http.createserver (APP). Listen (App.get (' Port '), function () {
Console.log (New Date ());
Console.log (' Express server listening on port ' + app.get (' port '));
});
settimeout (function () {
Console.log (New Date ());
throw new error (' APP is error from inner! ');
},10*1000);
Start with node command
Copy Code code as follows:
~ Node App.js
Thu Sep 2013 16:08:44 gmt+0800 (CST)
Express Server listening on port 3000
Thu Sep 2013 16:08:54 gmt+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new error (' APP is error from inner! ');
^
Error:app is error from inner!
At Null._ontimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
At timer.listontimeout [as OnTimeOut] (timers.js:110:15)
10 seconds later, the node process was hung due to an internal error.
Starting with the Forever command
Copy Code code as follows:
~ mkdir Logs
~ chmod 777-r Logs
~ Forever-p. -L./logs/access.log-e./logs/error.log start App.js
# Check error log
~ Cat Logs/access.log ls
Thu Sep 2013 16:15:02 gmt+0800 (CST)
Express Server listening on port 3000
Thu Sep 2013 16:15:12 gmt+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new error (' APP is error from inner! ');
^
Error:app is error from inner!
At Null._ontimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
At timer.listontimeout [as OnTimeOut] (timers.js:110:15)
Error:forever detected script exited with Code:8
Error:forever restarting script for 1 time
Thu Sep 2013 16:15:13 gmt+0800 (CST)
Express Server listening on port 3000
Thu Sep 2013 16:15:23 gmt+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new error (' app is Error from inner! ');
^
Error:app is Error from inner!
at Null._ontimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
at Timer.listontimeout [as OnTimeOut] (timers.js:110:15)
Error:forever detected script exited with Code:8
Error:for Ever restarting script for 2 time
Thu Sep 2013 16:15:23 gmt+0800 (CST)
Express server listening on port 3000
T Hu Sep 2013 16:15:33 gmt+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new error (' app is Error from inner! ');
^
Error:app is Error from inner!
at Null._ontimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
at Timer.listontimeout [as OnTimeOut] (timers.js:110:15)
Error:forever detected script exited with Code:8
Error:for Ever restarting script for 3 time
Thu Sep 2013 16:15:33 gmt+0800 (CST)
Express server listening on port 3000
T Hu Sep 2013 16:15:43 gmt+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new error (' APP is error from inner! ');
^
Error:app is error from inner!
At Null._ontimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
At timer.listontimeout [as OnTimeOut] (timers.js:110:15)
Error:forever detected script exited with Code:8
Error:forever restarting script for 4 time
We found that every 10 seconds, node inside the hang out, and then be forever reboot!!
Through the list we manually refresh several times can also see the PID is changed.
Copy Code code as follows:
~ Forever List
Info:forever processes Running
Data:uid command Script forever pid logfile Uptime
Data: [0] smtt/usr/local/bin/node app.js 18444 18579 logs/access.log 0:0:0:7.211
~ Forever List
Info:forever processes Running
Data:uid command Script forever pid logfile Uptime
Data: [0] smtt/usr/local/bin/node app.js 18444 18579 logs/access.log 0:0:0:8.921
~ Forever List
Info:forever processes Running
Data:uid command Script forever pid logfile Uptime
Data: [0] smtt/usr/local/bin/node app.js 18444 18604 logs/access.log 0:0:0:0.177
~ Forever List
Info:forever processes Running
Data:uid command Script forever pid logfile Uptime
Data: [0] smtt/usr/local/bin/node app.js 18444 18604 logs/access.log 0:0:0:2.206
This forever helps us complete a few of the more important server management features: "Start/Stop Command simple", "Support Hot Deployment", "Downtime restart", "Monitoring interface and log".
The steps to configure the script (/etc/init/nodejs-xx.conf) are omitted compared to upstart management. Other functions, but also a step closer to the use to know.
VII. development environment and start-up configuration of production environment
Development environment
Copy Code code as follows:
~ cd/home/conan/nodejs/nodejs-forever/
~ Forever-p. -L./logs/access.log-e./logs/error.log-a-W Start app.js
Production environment
Copy Code code as follows:
~ Export Log=/var/log/nodejs/project
~ Export Pid=/var/log/nodejs/project/forever.pid
~ Export App_path=/home/conan/nodejs/nodejs-forever
~ Export app= $APP _path/app.js
~ forever-p $APP _path-l $LOG/access.log-e $LOG/error.log-o $LOG/out.log-a--pidfile $PID start $APP