Erlang. Start remote shell error - erlang

I tried to start remote shell and get a following error:
*** ERROR: Shell process terminated! (^G to start new job) ***
Details:
1. Start erlang on a computer a2-x201:
erl -sname a#a2-x201
Erlang R14B (erts-5.8.1) [source] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.1 (abort with ^G)
(a#a2-x201)1> nodes().
[]
Start erlang on a computer a2-asrock
erl -sname b#a2-asrock
Establish link between nodes using computer a2-x201:
(a#a2-x201)2> net_adm:ping('b#a2-asrock').
pong
(a#a2-x201)3> nodes().
['b#a2-asrock']
Everything looks ok at the moment.
Starting remote shell on a2-asrock from a2-x201
CTRL+ G
-->r 'b2#a2-asrock'
-->j
1 {shell,start,[init]}
2* {'b2#a2-asrock',shell,start,[]}
-->c
* ERROR: Shell process terminated! (^G to start new job) *
What is wrong?

You are doing remoting on 'b2#a2-asrock' instead of 'b#a2-rock' which is the name of the created node. You're pinging 'b#a2-asrock' but you're trying to connect to 'b2#a2-asrock' instead. Seems that you have a typo in there.

Related

Why does net_kernel:monitor_nodes/2 not deliver nodeup/nodedown messages for sname nodes?

I start up a master node with a short name and get it running a process to monitor for node up and down messages.
> erl -sname master -cookie monster
Erlang R15B03 (erts-5.9.3) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false] [dtrace]
Eshell V5.9.3 (abort with ^G)
(master#pencil)1> c("/tmp/monitor.erl").
{ok,monitor}
(master#pencil)2> Pid = monitor:start().
<0.44.0>
(master#pencil)3> Pid ! running.
RECV :: running
running
(master#pencil)4> net_adm:names().
{ok,[{"master",52564}]}
At this point only the master node is running. I startup the second node on the same machine:
> erl -sname client -cookie monster
Erlang R15B03 (erts-5.9.3) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false] [dtrace]
Eshell V5.9.3 (abort with ^G)
(client#pencil)1>
and wait for a minute, just incase I'm reading the docs wrong and there's a complication with the net tickrate. Nothing, so on master I force the connection:
(master#pencil)5> net_adm:names().
{ok,[{"master",52564},{"client",52579}]}
(master#pencil)6>
and nothing from my little monitor process. Now, if I do the same thing but use long names--that is -name--this works just fine. I'm surprised, though, as the net_kernel docs don't mention that. What's the deal?
Here's the monitor.erl referenced above:
-module(monitor).
-export([start/0]).
start() ->
spawn_link(fun init_loop/0).
%%%===================================================================
%%% Internal Functions
%%%===================================================================
init_loop() ->
net_kernel:monitor_nodes(true, []),
loop().
loop() ->
receive
Msg -> io:format(user, "RECV :: ~p~n", [Msg])
end,
loop().
net_kernel:monitor_nodes/2 definitely does deliver nodeup/nodedown messages for nodes with either short and long names.
However, the nodeup message is only delivered when the node is connected, as mentioned in the documentation. Why you got the nodeup message with -name is a mystery (and couldn't be reproduced here) as net_adm:names/0 does not connect nodes at all. It only connects to epmd to obtain the list of locally registered nodes. It will even list nodes with a different cookie.
If you connect the client to the master (or the other way around) with net_adm:ping/1 (or an rpc call), the monitoring process will receive the nodeup message.

Using `net_adm` modules functions for localhost development nodes

I'm poking around with the net_adm module and can't seem to get any joy working with localhost nodes. My machine is named:
> hostname
walden
and I start up a few local short-named nodes:
> erl -sname one -setcookie monster
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.1 (abort with ^G)
(one#walden)1>
and
> erl -sname two -setcookie monster
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.1 (abort with ^G)
(two#walden)1>
So far, so good.
(two#walden)1> net_adm:names().
{ok,[{"two",63489},{"one",63491}]}
(two#walden)2> net_adm:localhost().
"walden"
(two#walden)3> net_adm:names().
{ok,[{"two",63489},{"one",63491}]}
Both of the local nodes are registered but they aren't connected, which is expected.
(two#walden)4> nodes().
[]
Back on node one:
(one#walden)1> net_adm:names(two).
{error,nxdomain}
(one#walden)2> net_adm:names('two#walden').
{error,nxdomain}
(one#walden)3> net_adm:dns_hostname(two).
{error,two}
(one#walden)4> net_adm:dns_hostname(two#walden).
{error,two#walden}
Erlang correctly identifies my localhost as walden but dns_hostname/1 returns an error. Back on two:
(two#walden)5> net_adm:ping(one#walden).
pong
(two#walden)6> nodes().
[one#walden]
and
(one#walden)5> nodes().
[two#walden]
Okay, bummer. It'd be really convenient to use a .hosts.erlang file for localhost development nodes--via net_adm:worlds/0--but it seems like I'm stuck a bit. Am I missing something or doing something glaringly wrong? Could my overall system be misconfigured?
You need to pass a hostname to names and dns_hostname, not a node name. It looks like your nodes are already connected to me, since the other shows up when you run nodes().

erlang gen_tcp:connect/3 not working with rpc:call/4, anyone knows why?

Could not solve this by myself
launched a new node A with ssh command
started a new node B
gen_tcp:connect/3 works on B, but rpc:call(B,gen_tcp,connect,Params) not works.
Both nodes are running on local laptop
and one node returns ok and the other node returns error.
I don't understand.
Anyone knows why?
~ $ssh allen#127.0.0.1 'erl -name loadtest#127.0.0.1 -detached -setcookie loadtest'
~ $erl -name allen#127.0.0.1 -setcookie loadtest
Erlang R14B03 (erts-5.8.4) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.4 (abort with ^G)
(allen#127.0.0.1)1> gen_tcp:connect("www.google.com",80,[]).
{ok,#Port<0.630>}
(allen#127.0.0.1)2> rpc:call('loadtest#127.0.0.1',gen_tcp,connect,["www.google.com",80,[]]).
{error,nxdomain}

Erlang ping node problem

I made in erlang shell:
1> node().
nonode#nohost
But
2> net_adm:ping(node()).
pang
Why? What's problem? Why not pong?
Thank you.
You didn't start Erlang with -name or -sname, which means that the distribution subsystem was not started. Try this:
$ erl -sname mynode
Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.3 (abort with ^G)
(mynode#foobar)1> node().
mynode#foobar
(mynode#foobar)2> net_adm:ping(node()).
pong
I am not 100% sure, but You started erl without "-name" oder "-sname". I believe net_adm:ping/1 only works in a distributed mode.
If you are trying to ping an erlang node but getting a pang.
Check the cookie with erlang:get_cookie() it will be some random string set the cookie of another node with erlang:set_cookie(Node, Cookie) or you could pass the cookie to the flag -setcookie
for example:
(foo#earth) erlang:get_cookie().
ASYRQKVNIFHWIIJQZIYN
(foo#earth) erlang:set_cookie(node(), 'secret cookie').
true
net:ping('mongooseim#localhost').
pong
Check out the docs

Accessing a Mnesia node from another Erlang shell while it is running

What is the best practice to accessing a single running mnesia node from another Erlang shell to only view data in the tables?
I tried opening two shells and pointing them to the same mnesia directory location which I realized was a very bad idea after finding this in the documentation.
-mnesia dir Directory. The name of the directory where all Mnesia data is stored. The name of the directory must be unique for the current node. Two nodes may, under no circumstances, share the same Mnesia directory. The results are totally unpredictable.
I think that easiest way is joining to remote shell. Just start erl with -remsh Node parameter
$ erl -sname foo
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.5 (abort with ^G)
(foo#hynek-notebook)1>
Another terminal:
$ erl -sname bar -remsh 'foo#hynek-notebook'
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.5 (abort with ^G)
(foo#hynek-notebook)1>
Another option is use powerful job control capability of erl (Press ^G)
$ erl -sname bar
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.5 (abort with ^G)
(bar#hynek-notebook)1>
User switch command
--> h
c [nn] - connect to job
i [nn] - interrupt job
k [nn] - kill job
j - list all jobs
s [shell] - start local shell
r [node [shell]] - start remote shell
q - quit erlang
? | h - this message
--> r 'foo#hynek-notebook'
--> j
1 {shell,start,[init]}
2* {'foo#hynek-notebook',shell,start,[]}
--> c
Eshell V5.7.5 (abort with ^G)
(foo#hynek-notebook)1>
User switch command
--> j
1 {shell,start,[init]}
2* {'foo#hynek-notebook',shell,start,[]}
--> c 1
(bar#hynek-notebook)1>
Note that you have to press Enter to show shell prompt if you are switching back to existing one.

Resources