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

孤街醉人 提交于 2019-12-03 16:08:08
Pascal

The best way is of course to set nodename in command line through "-sname node" or "-name node@host". But it is possible to use `net_kernel' module instead. It is described at 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

You can use the magical "emulator arguments" line (as described in the escript docs). 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!