简体   繁体   English

在命令行中使用basho rebar运行Erlang应用程序时,如何设置Erlang节点名称

[英]How set Erlang node name, when run an Erlang application by basho rebar from command line

I have compiled my Erlang application by using basho rebar which makes an stand-alone escript executable file. 我使用basho rebar编译了我的Erlang应用程序,它生成了一个独立的escript可执行文件。 I run it from command line like: ./myapp myconfig.config 我从命令行运行它,如:./ myapp myconfig.config

My questio is that how can I determine the Erlang node name that run my application. 我的问题是如何确定运行我的应用程序的Erlang节点名称。 When in my application I run 'node()' command, it returns by default "nonode@nohost" but I want to give my name to that node (eg mynode@domain.com), so when I run 'node()' in my application, I like to see 'mynode@domain.com' instead of 'nonode@nohost' 在我的应用程序中运行'node()'命令时,它默认返回“nonode @ nohost”,但我想将我的名字命名为该节点(例如mynode@domain.com),所以当我运行'node()'时在我的应用程序中,我喜欢看'mynode@domain.com'而不是'nonode @ nohost'

I know about "erlang -name 'mynode@domain.com'" but please consider I run the application from command line. 我知道“erlang -name'mynode@domain.com'”但请考虑从命令行运行应用程序。 I think an Erlang VM is run and terminate during the application life-time automatically. 我认为Erlang VM会在应用程序生命周期内自动运行和终止。

The best way is of course to set nodename in command line through "-sname node" or "-name node@host". 当然,最好的方法是通过“-sname node”或“-name node @ host”在命令行中设置nodename。 But it is possible to use `net_kernel' module instead. 但是可以使用`net_kernel'模块代替。 It is described at http://www.erlang.org/doc/man/net_kernel.html 它在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().
nonode@nohost
2> net_kernel:start([rumata, shortnames]).
{ok,<0.34.0>}
(rumata@rumata-osx)3> node().
'rumata@rumata-osx'
(rumata@rumata-osx)4> net_kernel:stop().
ok
5> node().
nonode@nohost
6> net_kernel:start(['rumata@myhost', longnames]). 
{ok,<0.44.0>}
(rumata@myhost)7> node().
rumata@myhost

I had a look at an application distributed with rebar (nitrogen). 我看了一下用钢筋(氮气)分发的应用程序。 They pass most of the vm arguments in a config file using the parameter -args_file: 它们使用参数-args_file传递配置文件中的大多数vm参数:

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

and in vm.args simply use the parameter -name to define the node name: 在vm.args中只需使用参数-name来定义节点名称:

-name nitrogen@127.0.0.1

You can use the magical "emulator arguments" line (as described in the escript docs ). 您可以使用神奇的“模拟器参数”行(如escript文档中所述 )。 For example: 例如:

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

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

The %%! %%! -prefixed line is treated as if it were passed to erl on the command line, allowing you to specify the node name from there. -prefixed行被视为在命令行上传递给erl ,允许您从那里指定节点名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM