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

3

8

I have compiled my Erlang application by using basho rebar which makes an stand-alone escript executable file. I run it from command line like: ./myapp myconfig.config

My questio is that how can I determine the Erlang node name that run my application. When in my application I run 'node()' command, it returns by default "nonode@nohost" but I want to give my name to that node (e.g. [email protected]), so when I run 'node()' in my application, I like to see '[email protected]' instead of 'nonode@nohost'

I know about "erlang -name '[email protected]'" but please consider I run the application from command line. I think an Erlang VM is run and terminate during the application life-time automatically.

Martinelli answered 29/12, 2012 at 2:0 Comment(0)
S
20

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
Stubstad answered 29/12, 2012 at 7:30 Comment(2)
For me it raises this error: Protocol: "inet_tcp": register error: {{badmatch,{error,econnrefused}}, [{inet_tcp_dist,listen,1, ....Martinelli
@SepehrSamini, you should start epmd before executing script: epmd -daemonFalmouth
F
0

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:

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:

-name [email protected]
Fern answered 29/12, 2012 at 7:48 Comment(0)
L
0

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.

Lunatic answered 29/12, 2012 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.