Erlang: starting a remote node programmatically

2019-03-20 03:50发布

问题:

I am aware that nodes can be started from the shell. What I am looking for is a way to start a remote node from within a module. I have searched, but have been able to find nothing.

Any help is appreciated.

回答1:

A bit more low level that pool is the slave(3) module. Pool builds upon the functionality in slave.

Use slave:start to start a new slave.

You should probably also specify -rsh ssh on the command-line.

So use pool if you need the kind of functionality it offers, if you need something different you can build it yourself out of slave.



回答2:

There's a pool(3) facility:

pool can be used to run a set of Erlang nodes as a pool of computational processors. It is organized as a master and a set of slave nodes..

pool:start/1,2 starts a new pool. The file .hosts.erlang is read to find host names where the pool nodes can be started. The slave nodes are started with slave:start/2,3, passing along Name and, if provided, Args. Name is used as the first part of the node names, Args is used to specify command line arguments.

With pool you get load distribution facility for free.

Master node may be started this way:

erl -sname poolmaster -rsh ssh

Key -rsh here specifies an alternative to rsh for starting a slave node on a remote host. We used SSH here. Make sure your box have working SSH keys, and you can authenticate to the remote hosts using these keys.

If there are no hosts in the file .hosts.erlang, then no slave nodes are started, and you can use slave:start/2,3 to start slave nodes manually passing arguments if needed.

You could, for example start a remote node:

Arg = "-mnesia_dir " ++ M,
slave:start(H, Name, Arg).

Ensure epmd(1) is up and running on the remote boxes in order to start Erlang nodes.

Hope that helps.