Erlang和Bash脚本(escript) [英] Erlang and bash scripting (escript)

查看:113
本文介绍了Erlang和Bash脚本(escript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Erlang中是一个新手,想将bash脚本与Erlang节点和功能合并。
我有一个Mnesia数据库,我们进入Erlang节点并运行多个函数,但是我想通过一些bash脚本运行这些功能,以便可以在其他地方使用这些bash脚本输出。
我的Erlang shell:-

I am very new in Erlang and want to merge bash script with Erlang node and function. I have one Mnesia Database where we go into Erlang node and run several function but i want to run those functions via some bash script so that I can use those bash script output elsewhere. My Erlang shell:-

sudo /opt/butler_server/bin/butler_server remote_console
Erlang/OTP 20 [erts-9.3.3.6] [source] [64-bit] [smp:28:28] [ds:28:28:10] [async-threads:10] 

Eshell V9.3.3.6  (abort with ^G)
(butler_server@localhost)1> 

在此shell中,当我们在函数下面运行时,它运行良好并且还提供了输出,请注意order_node, pps_manager是数据库中的模块名称,而get_by_id,send_order_related_notification,update_status_of_order_node是该模块中的功能。

And inside this shell when we run below function its running fine and giving output also, Please note order_node, pps_manager are the module name in the Database and get_by_id,send_order_related_notification,update_status_of_order_node are the fuction in that module.

f().

ChangeStatus =
fun() ->
        {ok,C2}=order_node:search_by([{status,equal,inventory_awaited}],key),

        io:format("Total Orders ~p", [length(C2)]),

        lists:foreach(fun(Id) ->
                              io:format("Orders ~p~n", [Id]),
                              order_node:update_status_of_order_node(Id,cancelled),
                              pps_manager:send_order_related_notification(element(2,order_node:get_by_id(Id)))
                      end, C2)
end.

ChangeStatus().

请让我知道如何使用bash脚本在erlang shell中运行以上代码片段。 / p>

Please let me know how i can run above code snippet in erlang shell by using a bash script.

推荐答案

使用escript时,您将启动一个新的Erlang VM,因此,如果要真正连接到正在运行的节点,则需要使用类似期望的东西。

When you use escript, you start a new Erlang VM, so if you want to really connect to a running node, you need to use something like expect.

但是,使用escript可以启动一个新节点并将其添加到正在运行的集群中,并借助 rpc模块,您可以在原始群集中运行代码:

However, with escript you can start a new node and add it to a running cluster, and with the help of the methods in the rpc module you can run code in the original cluster:

假设您有一个以<$ c开头的节点$ c> erl -name main@127.0.0.1 -setcookie cookie ,然后是脚本

Let's say that you have a node started with erl -name main@127.0.0.1 -setcookie cookie, then the escript

#!/usr/bin/env escript
%%! -name escript@127.0.0.1 -hidden -setcookie cookie
main([RemoteNodeString]) ->
    io:format("Escript node: ~p~n", [node()]),
    RemoteNode = list_to_atom(RemoteNodeString),
    io:format("~p's node(): ~p~n", [RemoteNode, rpc:call(RemoteNode, erlang, node, [])]),
    io:format("~p's nodes(): ~p~n", [RemoteNode, rpc:call(RemoteNode, erlang, nodes, [])]),
    ok.

将打印

$> ./test.escript main@127.0.0.1
Escript node: 'escript@127.0.0.1'
'main@127.0.0.1''s node(): 'main@127.0.0.1'
'main@127.0.0.1''s nodes(): []

(注意main -hidden标志),节点列表为空。

(Notice that main's nodes list is empty thanks to the -hidden flag).

除了他的脚本没有运行任何有用的代码外,这里还有三个问题:

Aside from the fact that his escript is not running any useful code, there are three issues here:

1

escript节点名称:由于erlang群集或同一主机中的名称必须唯一,因此如果escript可以同时运行两次,则可能会出现问题。您可以通过生成一个随机名称(在Erlang或bash中,该示例适用于Erlang)来解决它:

1
escript node name: As names in the erlang cluster or in the same host must be unique, this may be a problem if there's a chance that the escript is run twice concurrently. You can solve it by generating a random name (in Erlang or bash, the example is for Erlang):

#!/usr/bin/env escript
%%! -hidden -setcookie cookie
main([RemoteNodeString]) ->
    RandomTail = (<< <<($0 + rand:uniform(10)-1)>> || _ <- lists:seq(1,8) >>),
    RandomName = binary_to_atom(<<"escript", RandomTail/binary, "@127.0.0.1">>, utf8),
    io:format("Escript node: ~p~n", [RandomName]),
    net_kernel:start([RandomName, longnames]),
    RemoteNode = list_to_atom(RemoteNodeString),
    io:format("~p's node(): ~p~n", [RemoteNode, rpc:call(RemoteNode, erlang, node, [])]),
    io:format("~p's nodes(): ~p~n", [RemoteNode, rpc:call(RemoteNode, erlang, nodes, [])]),
    io:format("~p's nodes(hidden): ~p~n", [RemoteNode, rpc:call(RemoteNode, erlang, nodes, [hidden])]),
    ok.



$> ./test2.escript main@127.0.0.1 
Escript node: 'escript45560706@127.0.0.1'
'main@127.0.0.1''s node(): 'main@127.0.0.1'
'main@127.0.0.1''s nodes(): []
'main@127.0.0.1''s nodes(hidden): ['escript45560706@127.0.0.1']

但是,escript节点的名称是一个原子,该原子不是GC'd,尽管存在很高的原子限制。根据您的配置和使用方式,对您来说可能不是问题。

However, the escript node's name is an atom, atoms are not GC'd and the atoms limit, although really high, is present. It may or may not be an issue for you depending on your configuration and your use pattern.

2

原始节点名称和cookie:要连接到 main@127.0.0.1 ,您需要知道名称(如果它以长名或短名(如果主机部分带有点,则需要长名)和cookie。此信息位于 vm.args 文件中(或在shell行中)。

如果未设置cookie,则Erlang会创建一个随机的cookie并将其放置在 $ HOME 中。

2
Original node name and cookie: To connect to main@127.0.0.1 you need to know the name, if it was started with long or short names (if the host part has a dot, you need longnames) and the cookie. This information is in the vm.args file (or in the shell line).
If no cookie was set, Erlang creates a random one and places it in the $HOME.

3

网络与原始节点的连接:Erlang分布式协议需要4369端口(用于 EPMD )和节点的范围(可用于配置 inet_dist_listen_min inet_dist_listen_max 的范围)。

3
Network connection to the original node: Erlang distributed protocol requires the 4369 port (for EPMD) and a range for the nodes (avaliable for configuration with inet_dist_listen_min and inet_dist_listen_max) reachable.

准备好电子脚本后,可以从bash脚本中调用它,也可以从bash脚本中将其写入到tempfile中,然后再调用 env escript 对其进行操作( bash进程替换不起作用,因为escript使用< a href = https://erlang.org/doc/man/file.html#position-2 rel = nofollow noreferrer> file:position 和专业版cess替换是一条管道)。

Once you have the escript ready, you can call it from your bash script or you can write it to a tempfile from your bash script before calling env escript on it (bash process substitution does not work because escript uses file:position and the process substitution is a pipe).

这篇关于Erlang和Bash脚本(escript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆