2012-12-29 40 views
5

我使用basho rebar編譯了我的Erlang應用程序,該應用程序生成獨立的escript可執行文件。我從命令行運行它,如: ./myapp myconfig.config如何通過命令行通過basho rebar運行Erlang應用程序時設置Erlang節點名稱

我的問題是,如何確定運行我的應用程序的Erlang節點名稱。當我在我的應用程序中運行'node()'命令時,它默認返回「nonode @ nohost」,但我想給我的名字(例如[email protected]),所以當我運行'node()'在我的應用程序中,我喜歡看'[email protected]'而不是'nonode @ nohost'

我知道「erlang -name'[email protected]'」,但請考慮我從命令運行應用程序線。我認爲Erlang虛擬機會自動運行並在應用程序生命週期中終止。

回答

12

當然,最好的方法是通過「-sname node」或「-name node @ host」在命令行中設置nodename。 但是可以改用`net_kernel'模塊。它在http://www.erlang.org/doc/man/net_kernel.html

$ erl 
Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false] 

Eshell V5.9.1 (abort with ^G) 
1> node(). 
[email protected] 
2> net_kernel:start([rumata, shortnames]). 
{ok,<0.34.0>} 
([email protected])3> node(). 
'[email protected]' 
([email protected])4> net_kernel:stop(). 
ok 
5> node(). 
[email protected] 
6> net_kernel:start(['[email protected]', longnames]). 
{ok,<0.44.0>} 
([email protected])7> node(). 
[email protected] 
+3

對我來說,它引發了這個錯誤:Protocol:「inet_tcp」:register error:{{badmatch,{error,econnrefused}},[{inet_tcp_dist,listen,1,.... –

+1

@SepehrSamini, epmd'執行腳本之前:'epmd -daemon' – Lol4t0

0

描述我看看在帶加強筋(氮)的分佈式應用程序。

erts-5.9\bin\werl -pa %PA% -boot releases/2.1.0/nitrogen -embedded -config etc/app.generated.config -args_file etc/vm.args 

和vm.args簡單地使用參數-name定義節點名稱:

-name [email protected] 
0

您可以使用這些參數傳遞-args_file大部分的VM參數的配置文件使用神奇的「仿真器參數」行(如escript docs中所述)。例如:

#!/usr/bin/env escript 
%%! -sname ohai 

main(_Args) -> 
    io:format("I am: ~p~n", [node()]). 

,就好像它傳遞給erl在命令行上,可以讓你從那裏指定節點名稱的%%! -prefixed行處理。