LoginSignup
111
118

More than 5 years have passed since last update.

Node.js 自動再起動モジュール

Last updated at Posted at 2013-08-18

Node.js 自動再起動モジュールのまとめ

シンプルなプロセス管理ツール系

 ファイルの変更を監視し、変更があるとサーバの再起動をかける

node-dev、hotnode、alwaysなど

  • 起動
>モジュール名 app.js
例) node-dev app.js

高機能プロセス管理モジュール

 ファイル監視のみでなく、ログ管理やモジュール管理機能などを持っている

forever

  • 起動
> forever start app.js
  • 実行中スクリプトの表示
> forever list
  • 実行中スクリプトの停止や再起動
> forever stop
> forever stopall
> forever restart
  • アクション
start               Start SCRIPT as a daemon
stop                Stop the daemon SCRIPT
stopall             Stop all running forever scripts
restart             Restart the daemon SCRIPT
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
  • オプション
-m  MAX          Only run the specified script MAX times スクリプトの起動回数制限 
-l  LOGFILE      Logs the forever output to LOGFILE foevert本体のログ
-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) 実行するコマンド(デフォルトはnode)
-a, --append     Append logs ログを追記する 
--pidfile        The pid file
--sourceDir      The source directory for which SCRIPT is relative to
--minUptime      Minimum uptime (millis) for a script to not be considered "spinning" 最低使用時間(ミリ秒)
--spinSleepTime  Time to wait (millis) between launches of a spinning script. 実行中スクリプトの起動待ち時間
--plain          Disable command line colors
-d, --debug      Forces forever to log debug output
-v, --verbose    Turns on the verbose messages from Forever
-s, --silent     stdout/stderrへの出力を抑制する Run the child script silencing stdout and stderr
-w, --watch      Watch for file changes ファイル変更を監視する 
-h, --help       You're staring at it

node.jsスクリプトをforeverでデーモン化する
さくらVPSにforeverをインストールしNode.jsスクリプトをデーモン化

pm2

foreverに比べ、CPUに合わせてクラスタリングする機能などが追加されている

【Node.js】foreverより高機能なpm2でデーモン化
Goodbye node-forever, hello PM2

その他

up

他のモジュールとは違い再起動しないで変更を反映する
ただし、性能が若干劣化する

  • 起動
> up --watch --port 3000 app.js

Smashing Node.js に書いてあるEffective Node.jsなこと(第二弾)

 比較

node-dev hotnode always forever pm2 up
自動再起動 △※1

※1 再起動は行わないが変更内容は反映される

機能的には、node-dev、hotnode、alwaysはシンプルで、forever、pm2は高機能。
ローカル環境でファイルの修正を監視してサーバの再起動をかけるだけなら前者で十分。
後者はどちらかと言うと、本番環境向き。

111
118
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
111
118