接下來,就讓我們看看forever能不能實現目標。
一、forever介紹
forever是一個簡單的命令式nodejs的守護進程,能夠啟動,停止,重啟App應用。forever完全基于命令行操作,在forever進程之下,創建node的子進程,通過monitor監控node子進程的運行情況,一旦文件更新,或者進程掛掉,forever會自動重啟node服務器,確保應用正常運行。
二、 forever安裝
全局安裝forever
~ 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
查看forever幫助
~ 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:
我們看到forever支持的命令和配置項確實不少,應該是偏命令行的管理工具。
三、forever命令行的中文解釋
子命令actions:
start:啟動守護進程
stop:停止守護進程
stopall:停止所有的forever進程
restart:重啟守護進程
restartall:重啟所有的foever進程
list:列表顯示forever進程
config:列出所有的用戶配置項
set <key> <val>: 設置用戶配置項
clear <key>: 清楚用戶配置項
logs: 列出所有forever進程的日志
logs <script|index>: 顯示最新的日志
columns add <col>: 自定義指標到forever list
columns rm <col>: 刪除forever list的指標
columns set<cols>: 設置所有的指標到forever list
cleanlogs: 刪除所有的forever歷史日志
配置參數options: