一、概述
简单的使用node xxx.js
启动 nodejs 服务。nodejs 会一直运行,但是当我们关闭启动窗口的时候 nodejs 就自动退出服务了。有什么办法可以使退出窗口后 nodejs 服务仍然再后台运行呢? 没错,用forever
就可以,forever
则可以在 cmd 或 ssh 连接断开时,让项目一直运行,而且可以在项目崩溃时自动重启。
下面就简单介绍下他的安装和使用。
二、安装与使用步骤
1.安装
npm install forever -g #全局安装
2.使用
##启动相关
#1. 简单的启动
forever start app.js
#2. 指定forever信息输出文件,当然,默认它会放到~/.forever/forever.log
forever start -l forever.log app.js
#3. 指定app.js中的日志信息和错误日志输出文件,
#-o 就是console.log输出的信息,-e 就是console.error输出的信息
forever start -o out.log -e err.log app.js
#4. 追加日志,forever默认是不能覆盖上次的启动日志,
#所以如果第二次启动不加-a,则会不让运行
forever start -l forever.log -a app.js
#5. 监听当前文件夹下的所有文件改动,当文件有变动时重启服务
forever start -w app.js
#6. 显示所有运行的服务
forever list
##停止操作
#1. 停止所有运行的node App
forever stopall
#2. 停止其中一个node App
forever stop app.js
##当然还可以这样
#forever list 找到对应的id,然后:
forever stop [id]
##重启操作
#1. 启动所有
forever restartall
#2. 重启其中一个node App
forever restart app.js
##开发和线上建议配置
#开发环境下
NODE_ENV=development forever start -w server.js
NODE_ENV=development forever start -l forever.log -e err.log -a app.js
#线上环境下
NODE_ENV=production forever start -w server.js
NODE_ENV=production forever start -l ~/.forever/forever.log -e ~/.forever/err.log -w -a app.js
3.帮助手册
$ forever --help
usage: forever [action] [options] SCRIPT [script-options]
Monitors the script specified in the current process or as a daemon
actions:
start Start SCRIPT as a daemon
stop Stop the daemon SCRIPT by Id|Uid|Pid|Index|Script
stopall Stop all running forever scripts
restart Restart the daemon SCRIPT
restartall Restart all running forever scripts
list List all running forever scripts
config Lists all forever user configuration
set <key> <val> Sets the specified forever config <key>
clear <key> Clears the specified forever config <key>
logs Lists log files for all forever processes
logs <script|index> Tails the logs for <script|index>
columns add <col> Adds the specified column to the output in `forever list`
columns rm <col> Removed the specified column from the output in `forever list`
columns set <cols> Set all columns for the output in `forever list`
cleanlogs [CAREFUL] Deletes all historical forever log files
options:
-m MAX Only run the specified script MAX times
-l LOGFILE Logs the forever output to LOGFILE
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
-p PATH Base path for all forever related files (pid files, etc.)
-c COMMAND COMMAND to execute (defaults to node)
-a, --append Append logs
-f, --fifo Stream logs to stdout
-n, --number Number of log lines to print
--pidFile The pid file
--uid Process uid, useful as a namespace for processes (must wrap in a string)
e.g. forever start --uid "production" app.js
forever stop production
--sourceDir The source directory for which SCRIPT is relative to
--workingDir The working directory in which SCRIPT will execute
--minUptime Minimum uptime (millis) for a script to not be considered "spinning"
--spinSleepTime Time to wait (millis) between launches of a spinning script.
--colors --no-colors will disable output coloring
--plain Disable command line colors
-d, --debug Forces forever to log debug output
-v, --verbose Turns on the verbose messages from Forever
-s, --silent Run the child script silencing stdout and stderr
-w, --watch Watch for file changes
--watchDirectory Top-level directory to watch from
--watchIgnore To ignore pattern when watch is enabled (multiple option is allowed)
--killSignal Support exit signal customization (default is SIGKILL),
used for restarting script gracefully e.g. --killSignal=SIGTERM
-h, --help You're staring at it
[Long Running Process]
The forever process will continue to run outputting log messages to the console.
ex. forever -o out.log -e err.log my-script.js
[Daemon]
The forever process will run as a daemon which will make the target process start
in the background. This is extremely useful for remote starting simple node.js scripts
without using nohup. It is recommended to run start with -o -l, & -e.
ex. forever start -l forever.log -o out.log -e err.log my-daemon.js
forever stop my-daemon.js