このユーティリティはWindows NT/2000/XP(R)固有のものです。erlsrvを用いることで、Erlang エミュレータをサービスとして動作させることができます。よって、ユーザが再起動の度にログインをして、Erlangエミュレータを立ち上げる必要がなくなります。この方法で起動されたエミュレータは、Windows(R) サービスアプレットによって、その他のサービスと同様に管理されます。
実際のサービスと同様に、erlsrv は、サービス登録、変更、開始、停止のためのコマンドラインインターフェースを提供します。 As well as being the actual service, erlsrv also provides a command line interface for registering, changing, starting and stopping services.
サービスを管理するには、Administrator 権限でマシンにログインする必要があります。Erlang マシン自体は、デフォルトでローカル特権で動作します。起動時の特権レベルは、Windows(R)のサービスアプレットで変更することができます。
サービスから起動されたプロセスは、タスクマネージャにより"殺され"る可能性がありますます。サービスから起動されたエミュレータが殺されると、"OnFail"アクションが起動し、再起動することになるでしょう。
The following parameters may be specified for each Erlang service: Erlang サービスのパラメータとして、以下のものを指定することができます:
release handling が使用されているようであれば、常にignoreがセットされているのが望ましいです。 サービス停止時に再度サービスを起動するには、 heart を使用してください。
システムが release handling を使用しているようであれば、start_erl.exe のようなプログラムがセットされるべきです。
consoleDebugType は、どんな理由であれ、production に使用するべきではありません。このオプションは、開発中のErlang サービスのデバッグを便利にするオプションに すぎません。new や reuse オプションは製品用のシステムに便利なオプションように見えます。しかし、システム管理者は、サービス稼働中、ログが無制限に増え続けることを考慮に入れておく必要があり、再起動中をのぞいて、ログを削除することはできません。まとめると、DebugType はあくまでも「デバッグ用のオプション」だということです。製品用のログを見るには、Erlangに搭載されている標準のログ機能を使用してください。 The consoleDebugType is not in any way intended for production. It is only a convenient way to debug Erlang services during development. The new and reuse options might seem convenient to have in a production system, but one has to take into account that the logs will grow indefinitely during the systems lifetime and there is no way, short of restarting the service, to truncate those logs. In short, the DebugType is intended for debugging only. Logs during production are better produced with the standard Erlang logging facilities.
release handling を使用するサービス名は、 NodeName_Release という規則に従う必要があります。ここで、 NodeName は、@を含めない Erlang ノード名で、 Release はアプリケーションの現在のバージョンです。
The naming of the service in a system that uses release handling has to follow the convention NodeName_Release, where NodeName is the first part of the Erlang nodename (up to, but not including the "@") and Release is the current release of the application.
erlsrv {set | add} <service-name> [<service options>]
set と add コマンドは、 それぞれErlang サービスを追加/修正します。add コマンドの最も単純な例は、全てデフォルト値を利用して、オプションを全くつけないというものです。サービス名はシステムから自動的に命名されます(mandatory)。 The set and add commands adds or modifies a Erlang service respectively. The simplest form of an add command would be completely without options in which case all default values (described above) apply. The service name is mandatory.
全てのオプションは、オプションに値なしでコマンドに渡すことができます。 この場合、値はデフォルト値が使用されます。オプションに値を与えるときは、 デフォルト値を利用しないときのみ です。erlsrv set myservice -prio -arg は優先度にデフォルト値を設定して、全ての引数を取り除きます。 Every option can be given without parameters, in which case the default value is applied. Values to the options are supplied only when the default should not be used (i.e. erlsrv set myservice -prio -arg sets the default priority and removes all arguments).
以下のオプションを利用することができます:
erlsrv {start | stop | disable | enable} <service-name>
ここでコマンドを利用する理由は、「便利だから」という理由だけです。 通常、サービスの状態を管理するにはコントロールパネルのサービスアプレットを利用します。 start と stop コマンドはサービスを開始/停止するためにサービスマネージャとやりとりを行います。コマンドはサービスが実際に停止/開始するまで待ちます。サービスが無効化された上で、停止していない場合は、サービスが実際に停止するまで「無効の状態になった」と見なしません。サービスが自動モード(automatic mode)でこのコマンドが使用されると、ブート時にサービスが開始します。このコマンドはサービスをマニュアルに設定することができません。 These commands are only added for convenience, the normal way to manipulate the state of a service is through the control panels services applet. The start and stop commands communicates with the service manager for stopping and starting a service. The commands wait until the service is actually stopped or started. When disabling a service, it is not stopped, the disabled state will not take effect until the service actually is stopped. Enabling a service sets it in automatic mode, that is started at boot. This command cannot set the service to manual.
erlsrv remove <service-name>
このコマンドは登録されているオプションとともに、サービスを完全に削除します。remove コマンドを使用する前には、サービスは停止している必要があります。 This command removes the service completely with all its registered options. It will be stopped before it is removed.
erlsrv list [<service-name>]
サービス名(<service-name>)が指定されていない場合、Erlangサービス名の一覧が表示されます。もしサービス名が指定されると、サービスに与えられているオプションが全て表示されます。
Erlangマシンの環境は、2つの特殊な変数によって管理されています。ERLSRV_SERVICE_名前はマシンが起動したサービスの名前です。 ERLSRV_EXECUTABLE はサービスを開始するのに使用する erlsrv.exe への完全パスです。この2つの変数は、サービスを立ち上げるための核となるユーザ定義のコマンドを作成するのに、とても便利です。サービスの再起動に利用するコマンドファイルは、以下のように簡単に記述することができます:
The environment of an Erlang machine started as a service will contain two special variables, ERLSRV_SERVICE_名前, which is the name of the service that started the machine and ERLSRV_EXECUTABLE which is the full path to the erlsrv.exe that can be used to manipulate the service. This will come in handy when defining a heart command for your service. A command file for restarting a service will simply look like this:
@echo off %ERLSRV_EXECUTABLE% stop %ERLSRV_SERVICE_名前% %ERLSRV_EXECUTABLE% start %ERLSRV_SERVICE_名前%
これで、このコマンドファイルは、核となるコマンドとして設定されたことになります。 This command file is then set as heart command.
この2つの環境変数は、起動中のサービスを検知したり、ポートプログラム(何らかのイベントを待ち受けしているプログラム)に、ログアウト時に発生する制御イベントに対する挙動を定義したい場合にも利用可能です。
プログラムがサービスのコンテキストで動作しており、端末を操作しているユーザがログオフしたとき、サービスは全てのプログラムに対して送信される制御イベントをハンドルする必要があります。コンソールサブシステム内で動作しているアプリケーション(通常はポートプログラム)は、win32 API の SetConsoleCtrlHandlr を制御ハンドラとして利用しており、このハンドラが CTRL_LOGOFF_EVENT のイベントに対して TRUE を返します。その他のアプリケーションは、 WM_ENDSESSION と WM_QUERYENDSESSION をデフォルトのウインドウプロシージャに渡します。以下に、Cで書かれた簡単なコンソール制御ハンドラの例を示します:
#include <windows.h>
/*
** A Console control handler that ignores the log off events,
** and lets the default handler take care of other events.
*/
BOOL WINAPI service_aware_handler(DWORD ctrl){
if(ctrl == CTRL_LOGOFF_EVENT)
return TRUE;
return FALSE;
}
void initialize_handler(void){
char buffer[2];
/*
* We assume we are running as a service if this
* environment variable is defined
*/
if(GetEnvironmentVariable("ERLSRV_SERVICE_名前",buffer,
(DWORD) 2)){
/*
** Actually set the control handler
*/
SetConsoleCtrlHandler(&service_aware_handler, TRUE);
}
}
オプションは Unixライクなフォーマットで与えられますが、オプションやコマンドの大文字/小文字は関係ありません。また、オプションを利用するのに "-" だけでなく "/" も利用することができます。 Even though the options are described in a Unix-like format, the case of the options or commands is not relevant, and the "/" character for options can be used as well as the "-" character.
プログラムが、エミュレータの binディレクトリの中にあることに注意してください。 Erlang のルートディレクトリ直下の binディレクトリではありません。この理由は、起動中のシステム上のエミュレータをアップグレードする際に、微妙な問題が生じるためです。 新しいバージョンのランタイムシステムで既に存在する(、恐らく使用中の)実行ファイルを上書きするべきではありません。 Note that the program resides in the emulators bin-directory, not in the bin-directory directly under the Erlang root. The reasons for this are the subtle problem of upgrading the emulator on a running system, where a new version of the runtime system should not need to overwrite existing (and probably used) executables.
Erlang サービスをより簡単に操作するには、 erlsrvのバイナリを<erlang_root\bin から <erlang_root>\erts-<version>\bin ディレクトリへ移動してください。 ^fIos:find_executable/1 という Erlang の関数を利用することで、 erlsrv プログラムが Erlang 内部から利用可能になります。
To easily manipulate the Erlang services, put the <erlang_root>\erts-<version>\bin directory in the path instead of <erlang_root>\bin. The erlsrv program can be found from inside Erlang by using the os:find_executable/1 Erlang function.
release handling を有効化するには、 Erlang マシンの start_erl を使用してください。ここでもサービス名が非常に重要であることは、上記で述べた通りです。 For release handling to work, use start_erl as the Erlang machine. It is also worth mentioning again that the name of the service is significant (see above).
start_erl(1), release_handler(3)