Node.js package : forever

forever為一套node.js維運套件
透過監控程式健康狀態來管理node.js程式,並在異常關閉時候重新啓動您的程式,這樣就不用怕程式因為沒有handle的exception導致異常關閉時候不能提供服務拉!

forever的安裝與使用相當簡單:

1.首先,安裝forever套件(當然,您需要先有node.js與npm套件拉!),其中-g很重要,因為您必須要安裝到系統目錄,才能夠支援在任何環境下啟動forever程式
# npm install forever -g

2.使用forever管理您的node.js程式: (這邊假設我們建立了一個express專案,app.js的路徑是/tmp/TestPrj/app.js)
啟動您的node.js程式:forever start [node.js application path]
# forever start /tmp/TestPrj/app.js 
info:   Forever processing file: /tmp/TestPrj/app.js

檢視已啓動的forever程式(就是您使用forever啓動的node.js程式):forever list
# forever list
info:   Forever processes running
data:       uid  command script              forever pid   logfile                          uptime        
data:   [0] P0Fa node    /tmp/TestPrj/app.js 50062   50079 /Users/simonsu/.forever/P0Fa.log 0:1:49:10.335 

關閉已啓動的Node程式:forever stop [forever process id]
# forever stop 0
info:   Forever stopped process:
data:       uid  command script              forever pid   logfile                          uptime       
[0] P0Fa node    /tmp/TestPrj/app.js 50062   50079 /Users/simonsu/.forever/P0Fa.log 0:2:7:29.415


如果覺得查id再做關閉麻煩,您也可以使用:forever stop [node.js application path]

# forever stop /tmp/TestPrj/app.js
info:   Forever stopped process:
data:       uid  command script              forever pid   logfile                          uptime       
[0] r0Gh node    /tmp/TestPrj/app.js 51604   51605 /Users/simonsu/.forever/r0Gh.log 0:0:0:11.932 

其他與forever相關的指令與參數,可以使用forever來查詢:
# forever
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:     --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:     --plain          Disable command line 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:     -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:   

這個網誌中的熱門文章

Bash判斷參數是否存在