Nodejs进程管理模块forever详解
接下来,就让我们看看forever能不能实现目标。
一、forever介绍
forever是一个简单的命令式nodejs的守护进程,能够启动,停止,重启App应用。forever完全基于命令行操作,在forever进程之下,创建node的子进程,通过monitor监控node子进程的运行情况,一旦文件更新,或者进程挂掉,forever会自动重启node服务器,确保应用正常运行。
二、 forever安装
全局安装forever
~D:\workspace\javascript>npminstall-gforever 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:Monitorsthescriptspecifiedinthecurrentprocessorasadaemon help: help:actions: help:startStartSCRIPTasadaemon help:stopStopthedaemonSCRIPT help:stopallStopallrunningforeverscripts help:restartRestartthedaemonSCRIPT help:restartallRestartallrunningforeverscripts help:listListallrunningforeverscripts help:configListsallforeveruserconfiguration help:set<key><val>Setsthespecifiedforeverconfig<key> help:clear<key>Clearsthespecifiedforeverconfig<key> help:logsListslogfilesforallforeverprocesses help:logs<script|index>Tailsthelogsfor<script|index> help:columnsadd<col>Addsthespecifiedcolumntotheoutputin`foreverlist` help:columnsrm<col>Removedthespecifiedcolumnfromtheoutputin`foreverlist` help:columnsset<cols>Setallcolumnsfortheoutputin`foreverlist` help:cleanlogs[CAREFUL]Deletesallhistoricalforeverlogfiles help: help:options: help:-mMAXOnlyrunthespecifiedscriptMAXtimes help:-lLOGFILELogstheforeveroutputtoLOGFILE help:-oOUTFILELogsstdoutfromchildscripttoOUTFILE help:-eERRFILELogsstderrfromchildscripttoERRFILE help:-pPATHBasepathforallforeverrelatedfiles(pidfiles,etc.) help:-cCOMMANDCOMMANDtoexecute(defaultstonode) help:-a,--appendAppendlogs help:-f,--fifoStreamlogstostdout help:-n,--numberNumberofloglinestoprint help:--pidFileThepidfile help:--sourceDirThesourcedirectoryforwhichSCRIPTisrelativeto help:--minUptimeMinimumuptime(millis)forascripttonotbeconsidered"spinning" help:--spinSleepTimeTimetowait(millis)betweenlaunchesofaspinningscript. help:--colors--no-colorswilldisableoutputcoloring help:--plainaliasof--no-colors help:-d,--debugForcesforevertologdebugoutput help:-v,--verboseTurnsontheverbosemessagesfromForever help:-s,--silentRunthechildscriptsilencingstdoutandstderr help:-w,--watchWatchforfilechanges help:--watchDirectoryTop-leveldirectorytowatchfrom help:--watchIgnoreToignorepatternwhenwatchisenabled(multipleoptionisallowed) help:-h,--helpYou'restaringatit help: help:[LongRunningProcess] help:Theforeverprocesswillcontinuetorunoutputtinglogmessagestotheconsole. help:ex.forever-oout.log-eerr.logmy-script.js help: help:[Daemon] help:Theforeverprocesswillrunasadaemonwhichwillmakethetargetprocessstart help:inthebackground.Thisisextremelyusefulforremotestartingsimplenode.jsscripts help:withoutusingnohup.Itisrecommendedtorunstartwith-o-l,&-e. help:ex.foreverstart-lforever.log-oout.log-eerr.logmy-daemon.js help:foreverstopmy-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>:显示最新的日志 columnsadd<col>:自定义指标到foreverlist columnsrm<col>:删除foreverlist的指标 columnsset<cols>:设置所有的指标到foreverlist cleanlogs:删除所有的forever历史日志
配置参数options:
-mMAX:运行指定脚本的次数 -lLOGFILE:输出日志到LOGFILE -oOUTFILE:输出控制台信息到OUTFILE -eERRFILE:输出控制台错误在ERRFILE -pPATH:根目录 -cCOMMAND:执行命令,默认是node -a,–append:合并日志 -f,–fifo:流式日志输出 -n,–number:日志打印行数 –pidFile:pid文件 –sourceDir:源代码目录 –minUptime:最小spinn更新时间(ms) –spinSleepTime:两次spin间隔时间 –colors:控制台输出着色 –plain:–no-colors的别名,控制台输出无色 -d,–debug:debug模式 -v,–verbose:打印详细输出 -s,–silent:不打印日志和错误信息 -w,–watch:监控文件改变 –watchDirectory:监控顶级目录 –watchIgnore:通过模式匹配忽略监控 -h,–help:命令行帮助信息
四、forever服务器管理
创建一个web项目(express3+ejs),使用forever管理服务器。
安装express3
~D:\workspace\javascript>express-enodejs-forever ~D:\workspace\javascript>cdnodejs-forever&&npminstall
通过forever启动应用
~D:\workspace\javascript\nodejs-forever>foreverstartapp.js warn: --minUptimenotset.Defaultingto:1000ms warn: --spinSleepTimenotset.Yourscriptwillexitifitdoesnotstayupforatleast1000ms info: Foreverprocessingfile:app.js
打开浏览器:http://localhost:3000,可以看到web界面
在win下面查看forever状态
~D:\workspace\javascript\nodejs-forever>foreverlist info: Noforeverprocessesrunning
~D:\workspace\javascript\nodejs-forever>foreverstopapp.js error: Forevercannotfindprocesswithindex:app.js