[node.js] node.js 로 생성한 스크립트를 서버 내 daemon 형태로 실행하기

1. 배경

  • node.js 로 실행한 코드를 콘솔로 접속하여 실행할 시 콘솔 접속 후에는 해당 프로그램이 종료된다.

  • cronjob 과 같은 배치성 프로그램을 daemon 형태로 지속 실행하기 위해서 forever 란 패키지를 이용해야한다.

2. 설치

  • npm으로 forever 패키지를 설치한다.

  • 서버 내 공용으로 사용될 예정으로 global 옵션인 -g 를 적용한다.

$ npm install -g forever

3. 사용 방법

  • forever command 를 이용하여 node.js 코드를 실행한다.

  • 사용 가능한 명령어는 help 를 참고 시 아래와 같다.

    $ 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 by Id|Uid|Pid|Index|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:     columns reset       Resets all columns to defaults for the output in `forever list`
    help:     cleanlogs           [CAREFUL] Deletes all historical forever log files

  • 실행 명령어 : forever start test_code.js

  • 확인 명령어 : forever list

    $ forever list
    info:   Forever processes running
    data:       uid command       script                 forever pid   id logfile                 uptime      
    data:   [0] QFcD test_code.js 39801   39808   /root/.forever/QFcD.log 0:0:0:53.367
  • 실행중지 명령어 : forever stop test_code.js 또는 forever stopall



다른 카테고리의 글 목록

node.js 카테고리의 포스트를 톺아봅니다