Starting a new node from inside elixir

How can I start a new node inside the elixir code?

I have tried Node.connect(node_name) but it halts the rest of the program. Is there any way that the node runs in the background?

Can I start it using System.cmd/3 ?

1 Like

You can use the :slave module, e.g.:

$ iex --sname one
# ...
iex(one@my-hostname)1> {:ok, hostname} = :inet.gethostname()
{:ok, 'my-hostname'}
iex(one@my-hostname)2> :slave.start(hostname, :two)
{:ok, :"two@my-hostname"}
iex(one@my-hostname)3> Node.list()                          

In OTP 25 there will be a new :peer module that I believe will eventually replace :slave


This will start slave node but I want to start a node. When I run it inside my elixir code, I get this error message

** (exit) :not_alive

I have this question, I do not want to start slave, I need to start a new node and maybe the second node is changed something like I put a new function and I need to compile before, but the main or one node should be the old source-compiled.

I can do this:

iex --name a@ -S mix
iex(a@> pid = spawn(fn -> System.cmd("iex", ["--name", "c@", "-S", "mix"]) end)
iex(a@> Node.list
iex(a@> Node.connect :"c@"
iex(a@> Node.list
[:"b@", :"c@"]
iex(a@> Node.spawn_link(:"c@", fn -> IO.inspect Hellow.test4() end) |> IO.inspect
"this is test 4"

But how can I know the new node of code is compiled or not? And if is there a compile error, how can be able to get the error?

I solved my problem with System.cmd("mix", ["deps.compile"], into:, if you have another suggestion, please let me know