Next, let’s see if forever can achieve the goal.
1. Forever introduction
forever is a simple imperative nodejs daemon that can start, stop and restart App applications. Forever is completely based on command line operations. Under the forever process, a node child process is created and the node child process is monitored through the monitor. Once the file is updated or the process is lapsed, forever will automatically restart the node server to ensure that the application is running normally.
2. Forever installation
Global installation forever
The code copy is as follows:
~ 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
Check out forever help
The code copy is 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 for 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 be considered "spinning"
help: --spinSleepTime Time to wait (millis) between launches of a spinning script.
help: --colors --no-colors will 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 is enabled (multiple option is allowed)
help: -h, --help You're staring at it
help:
help: [Long Running Process]
help: The forever process will 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 will run as a daemon which will make the target process start
help: in the background. This is extremely useful for 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 lot of command and configuration items, which should be a command-line management tool.
3. Chinese explanation of the forever command line
Subcommand actions:
The code copy is as follows:
start: Start the daemon process
stop:Stop the daemon
stopall: Stop all forever processes
restart:Restart the daemon
restartall: Restart all foever processes
list: list displays the forever process
config: List all user configuration items
set <key> <val>: Set user configuration items
clear <key>: Clear user configuration items
logs: lists logs for all forever processes
logs <script|index>: Show the latest log
columns add <col>: Custom metrics to forever list
columns rm <col>: Remove the forever list metric
columns set<cols>: Set all metrics to forever list
cleanlogs: delete all forever history logs
Configuration parameter options:
The code copy is as follows:
-m MAX: Number of times the specified script is 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 the command, the 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: The interval between two spins
colors: console output coloring
plain: no-colors alias, console output colorless
-d, debug: debug mode
-v, verbose: Print detailed output
-s, silent: Do not print logs and error messages
-w, watch: Monitor file changes
watchDirectory: Monitor the top directory
watchIgnore: Ignore monitoring via pattern matching
-h, help: Command line help information
4. Forever server management
Create a web project (express3+ejs) and use forever to manage the server.
Install express3
The code copy is as follows:
~ D:/workspace/javascript>express -e nodejs-forever
~ D:/workspace/javascript>cd nodejs-forever && npm install
Start the application via forever
The code copy is as follows:
~ D:/workspace/javascript/nodejs-forever>forever start app.js
warning: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
Open the browser: http://localhost:3000, you can see the web interface
Check the forever status under win
The code copy is 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 that the forever program was working wrong! ! The program is clearly in its running state, but it cannot be seen through the list. Next, switch to Linux Ubuntu and continue testing.
5. Forever manages servers in Ubuntu
Linux system environment
Linux: Ubuntu 12.04.2 64bit Server
Node: v0.11.2
Npm: 1.2.21
Initialization project: The installation command is not explained
The code copy is as follows:
~ cd /home/conan/nodejs
~ express -e nodejs-forever
~ cd nodejs-forever && npm install
~ sudo npm install forever -g
Start forever
Copy the code as follows: ~ forever start app.js
warning: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
Check node server status
Copy the 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 '-'? See 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 occupation
~ netstat -nltp|grep node
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 18299/node
Stop the server
Copy the 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 that it is normal in Linux Ubuntu environment.
6. Simulate server downtime
Two test solutions:
1. Use Linux commands to directly kill the node process
2. In the application, simulate exception exit
1). Use Linux commands to directly kill the node process
The code copy is as follows:
# Check the 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
# Look at the node process again, the node will 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 that killing the node process, forever will help us restart the node.
Kill forever monitor
The code copy is 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
# Kill the node process again
~ 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 forever's monitor. The monitor program did not restart automatically. After killing the node process, the node will not restart automatically.
2). In the application, simulate exception exit
Modify file: app.js
The code copy is 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 the code as follows: ~ node app.js
Thu Sep 26 2013 16:08:44 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 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)
After 10 seconds, the node process hangs due to an internal error.
Start with the forever command
The code copy is as follows:
~ mkdir logs
~ chmod 777 -R logs
~ forever -p . -l ./logs/access.log -e ./logs/error.log start app.js
# Check the error log
~ cat logs/access.log ls
Thu Sep 26 2013 16:15:02 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 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 26 2013 16:15:13 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 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: Forever restarting script for 2 time
Thu Sep 26 2013 16:15:23 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 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: Forever restarting script for 3 time
Thu Sep 26 2013 16:15:33 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 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, the node hangs up and then is restarted by forever! !
Through list, we can also see that the pid has changed several times.
The code copy is 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
In this way, forever helps us complete several important server management functions: "Simple start/stop command", "support hot deployment", "download restart", and "monitoring interface and logs".
Compared with upstart management, the steps to configure scripts (/etc/init/nodejs-xx.conf) are omitted. Other functions need to be used one step closer to know.
7. Startup configuration of development environment and production environment
Development Environment
The code copy is 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 the 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