본문 바로가기

프로그래밍/Node.js

[Node.js] Forever 란?

- 스크립트의 연속적인 실행을 보장하기 위해 사용하는 간단한 CLI 툴

 

*CLI: Command line interface의 약자로 텍스트 터미널을 통해 사용자와 컴퓨터가 상호 작용하는 방식을 뜻한다.

 

- npm: https://www.npmjs.com/package/forever

 

- 사용 방법: npm 을 통해 설치 후 커맨드라인에서 'forever <명령어> <대상>'의 형태로 사용

 

- 액션 및 옵션

 

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            DEPRECATED. Process uid, useful as a namespace for processes (must wrap in a string)

                     e.g. forever start --uid "production" app.js

                         forever stop production

    --id             DEPRECATED. Process id, similar to uid, useful as a namespace for processes (must wrap in a string)

                     e.g. forever start --id "test" app.js

                         forever stop test

    --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)

    -t, --killTree   Kills the entire child process tree on `stop`

    --killSignal     Support exit signal customization (default is SIGKILL),

                     used for restarting script gracefully e.g. --killSignal=SIGTERM

    -h, --help       You're staring at it

 

- 설정 파일 생성: json 형식의 파일을 통해 설정해서 사용할 수 있다.

- 설정 파일 예시

 

/ forever/development.json

{

    // Comments are supported

    "uid": "app",

    "append": true,

    "watch": true,

    "script": "index.js",

    "sourceDir": "/home/myuser/app",

    "logFile": "/home/myuser/logs/forever.log",

    "outFile": "/home/myuser/logs/out.log",

    "errFile": "/home/myuser/logs/error.log"

}

 

$ forever start ./forever/development.json

 

[

  {

    // App1

    "uid": "app1",

    "append": true,

    "watch": true,

    "script": "index.js",

    "sourceDir": "/home/myuser/app1"

  },

  {

    // App2

    "uid": "app2",

    "append": true,

    "watch": true,

    "script": "index.js",

    "sourceDir": "/home/myuser/app2",

    "args": ["--port", "8081"]

  }

]

 

- forever 모듈을 통해서 코드라인 내에서 사용할 수 있다.

- 내장 함수

  forever.load (config): Synchronously sets the specified configuration (config) for the forever module.

  forever.start (file, options): Starts a script with forever. 

  forever.startDaemon (file, options): Starts a script with forever as a daemon.

  forever.stop (index): Stops the forever daemon script at the specified index.

  forever.stopAll (format): Stops all forever scripts currently running.

  forever.list (format, callback): Returns a list of metadata objects about each process that is being run using forever.

  forever.tail (target, options, callback): Responds with the logs from the target script(s) from tail.

  forever.cleanUp (): Cleans up any extraneous forever *.pid files that are on the target system.

  forever.cleanLogsSync (processes): Removes all log files from the root forever directory that do not belong to current running forever processes.

  forever.startServer (monitor0, monitor1, ..., monitorN): Starts the forever HTTP server for communication with the forever CLI.