Run erlang from unix shell with complex options

I need to run complex erlang module function from unix shell

rpc:call('node@example.com', mnesia, dirty_first, [mytable])

How can i do this?

UPD:

i do test.escript

chmod +x test.escript

#!/usr/lib64/erlang/bin/escript
%%! -name 'test@example.com'
main(_Args) ->
    R = rpc:call('node@example.com', mnesia, dirty_first, [mytable]),
    io:format("~p~n",[R]).

and get {badrpc, nodedown}

but at startup

erl -name test@example.com
1> rpc:call('node@example.com', mnesia, dirty_first, [mytable]).
{my, data}.

I mean this works, but how to get escript to work proprely?

+3
source share
5 answers

I think escript might be interesting.

Edit: Some examples.

First for all the examples: somehow start the remote node.

            dannib@duval:~:> erl -sname bar
            (bar@duval)1> erlang:get_cookie().
            'KNKKCFPYMJUPIOLYPOAA'

Escript

1: create a file with a name hello.escriptwith content

            #!/usr/bin/env escript
            %%! -sname foo@duval -setcookie KNKKCFPYMJUPIOLYPOAA

            main(_String) ->
                Node = 'bar@duval',
                Mod = 'erlang',
                Fun = 'node', 
                Args = [], 
                    R = rpc:call(Node, Mod, Fun, Args),
                io:format("Hello there ~p~n",[R]).

, %%! -sname foo@bar node ( unode @nohost), cookie %%! -sname foo@duvel -setcookie KNKKCFPYMJUPIOLYPOAA, , {badrpc,nodedown}. , (erl_call -eval), node cookie.

2:

            $ chmod +x hello.escript
            $ ./hello.escript 
            Hello there bar@duval

Erl_call

1:

            $ erl_call -c 'KNKKCFPYMJUPIOLYPOAA' -a 'erlang node' -n bar@duval
            bar@duval

Eval

1:

            $ erl -sname foo -setcookie 'KNKKCFPYMJUPIOLYPOAA' 
            -eval 'io:format("Hello there ~p~n",[rpc:call(bar@duval,erlang, node, [])])'
            ... Eshell V5.7.4  (abort with ^G)
            (foo@duval)1> Hello there bar@duval

, , .

, cookie, cookie foo bar , .

, , , , erl_call. "" , imho- "" . _String escript script, erlang EVM. erl_call , , erlang node.

+4

erl_call - , :

erl_call / Erlang node. erl_interface . - Unix script Erlang node. rex Erlang, Erlang RPC. Erlang node.

- Erlang node, . , Erlang erl_call Erlang, ( Erlang).

.

+3

-eval erl:

$ erl -eval 'io:format("Hello, World!~n")'
+1

You can parse complex arguments with escript :

#!/usr/bin/env escript

main(String) ->
  {Node, Mod, Fun, Args} = parse_args(String),
  R = rpc:call(Node, Mod, Fun, Args),
  io:format("~p~n",[R]).
0
source

If your problem is how to install Erlang node in network mode (i.e. put the node in a distributed node), you can do something like

EPMD = code:root_dir() ++ "/bin/epmd &",
os:cmd(EPMD),
net_kernel:start([Sname, shortnames])

where Sname is your desired node name. Only after that you can start talking to another node, for example. RPC

0
source

Source: https://habr.com/ru/post/1781974/


All Articles