Nodejs process management module forever

  • 2020-03-30 03:08:33
  • OfStack

Next, let's see if forever can achieve its goal.

Introduction to forever

Forever is a simple imperative nodejs daemon that can start, stop, and restart apps. Forever operates entirely from the command line. Under the forever process, a node child is created, and the node child is monitored by monitor. Once the file is updated or the process is down, forever automatically restarts the node server to make sure the application is running.

Second,   Forever is installed

Global installation forever


~ D:workspacejavascript>npm install -g forever
D:toolkitnodejsforever -> D:toolkitnodejsnode_modulesforeverbinforever
D:toolkitnodejsforeverd -> D:toolkitnodejsnode_modulesforeverbinforeverd

Check out forever help

~ D:workspacejavascript>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:

Forever command line explanation in Chinese

Actions:


start: Start the daemon 
stop: Stop daemon 
stopall: Stop all forever process 
restart: Restart the daemon 
restartall: Restart all foever process 
list: The list shows forever process 
config: Lists all user configuration items 
set <key> <val>:  Set the user configuration item 
clear <key>:  Clear user configuration items 
logs:  List all forever Process log 
logs <script|index>:  Show the latest log 
columns add <col>:  Custom index to forever list
columns rm <col>:  delete forever list The indicators 
columns set<cols>:  Set all the indicators to forever list
cleanlogs:  Delete all forever The history log 

Configuration parameter options:

-m MAX:  Number of times to run the specified script 
-l LOGFILE:  Output log to LOGFILE
-o OUTFILE:  Output console information to OUTFILE
-e ERRFILE:  Output console error in ERRFILE
-p PATH:  The root directory 
-c COMMAND:  Execute the command, default is node
-a,  � append:  Combine log 
-f,  � fifo:  Stream log output 
-n,  � number:  Number of printed lines in the log 
 � pidFile: pid file 
 � sourceDir:  Source code directory 
 � minUptime:  The minimum spinn Update time (ms)
 � spinSleepTime:  two spin Time interval between 
 � colors:  Console output coloring 
 � plain:  � no-colors The console output is colorless 
-d,  � debug: debug model 
-v,  � verbose:  Print detailed output 
-s,  � silent:  Do not print logs and error messages 
-w,  � watch:  Monitor file changes 
 � watchDirectory:  Monitor top-level directory 
 � watchIgnore:  Ignore 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


~ D:workspacejavascript>express -e nodejs-forever
~ D:workspacejavascript>cd nodejs-forever && npm install

Launch the app through forever

~ D:workspacejavascriptnodejs-forever>forever start app.js
warn:    --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 a browser: http://localhost:3000 and you'll see the web interface

Check out forever status under win


~ D:workspacejavascriptnodejs-forever>forever list
info:    No forever processes running
~ D:workspacejavascriptnodejs-forever>forever stop app.js
error:   Forever cannot find process with index: app.js

We found that forever's program was not working correctly!! The program is clearly running state, through the list is not seen. Next, switch to Linux Ubuntu and continue testing.

Forever server management 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


~ cd /home/conan/nodejs
~ express -e nodejs-forever
~ cd nodejs-forever && npm install
~ sudo npm install forever -g

Start the forever
~ forever start app.js
warn:    --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

View the node server status
~ 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 processes 
~ 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
#  The port to take up 
~ netstat -nltp|grep node
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN      18299/node

Stop server

~ 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 the Linux Ubuntu environment.

Six, simulation server down

Two test schemes:

1. Directly kill the node process with the Linux command
2. In the application, the simulation exception exits

1). Kill the node process directly with the Linux command


#  To view node The 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
#  Take a look at node The process, node Automatic restart, 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 saw that by killing the node process, forever would help us restart node.

Kill forever's monitor


~ 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
#  To kill 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 forever's monitor, which did not restart automatically, and then killed the node process, which did not restart automatically.

2). In the application, the simulation exception exits
Modify file: app.js


~ 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 the node command
~ 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


~ 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, node dies internally and is rebooted by forever!!

Instead of upstart management, the configuration script steps are omitted (/etc/init.nodejs-xx.conf). Other functions, but also a further step to use just know.

Start configuration of development environment and production environment

The development environment


~ cd /home/conan/nodejs/nodejs-forever/
~ forever -p . -l ./logs/access.log -e ./logs/error.log -a -w start app.js

The production environment
~ 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


Related articles: