简体   繁体   English

Elixir:为使用mix escript.build创建的版本设置co​​okie和主机名

[英]Elixir: Set cookie and hostname for release created using mix escript.build

When running iex interactively, one can use 当以交互方式运行iex时,可以使用

iex --cookie <cookie> --name <hostname>

How do I set the same values for cookie and name when running an executable created using mix escript.build ? 运行使用mix escript.build创建的可执行文件时,如何为cookiename设置相同的值?

I figured out that I need to create a vm.args file with the following contents 我想我需要创建一个vm.args以下内容的vm.args文件

## Name of the node
-name name@host

## Cookie for distributed erlang
-setcookie cookie

So I created a vm.args file in the same directory as the executable file. 所以我在与可执行文件相同的目录中创建了一个vm.args文件。 But when I print Node.self() , I get :nonode@nohost . 但是当我打印Node.self() ,我得到:nonode@nohost

So where do I store vm.args so that it is read by the executable? 那么我在哪里存储vm.args以便可执行文件读取它?

As far as I know, vm.args is not read by escripts. 据我所知, vm.args不是由escripts读取的。 You have (at least) 2 options: 你有(至少)2个选择:

  1. Set these values in emu_args key passed to escript in project/0 in mix.exs : emu_args设置这些值,传递给escriptproject/0中的mix.exs

     def project do [app: :m, ..., escript: [main_module: M, emu_args: ["-name foo@bar -setcookie baz"]]] end 
  2. Parse the CLI arguments and set the values in your main function: 解析CLI参数并在main函数中设置值:

     defmodule M do def main([name, cookie]) do Node.start String.to_atom(name) Node.set_cookie String.to_atom(cookie) IO.inspect {Node.self, Node.get_cookie} end end 
     $ mix escript.build $ ./m foo@bar baz {:foo@bar, :baz} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM