简体   繁体   English

如何构建一个Elixir escript,它不会在执行后停止Erlang VM(如elixir --no-halt)

[英]How can I build an Elixir escript that does not halt the Erlang VM after execution (like elixir --no-halt)

I have a program that starts the application and then adds (children) workers to a supervisor. 我有一个启动应用程序的程序,然后将(子)工作者添加到主管。 Obviously after doing only that it has nothing more left to do and it halts (exits). 显然,在做了之后,它就没有什么可做的了,它就会停止(退出)。 So making it not halt the VM would allow the workers to work. 因此,让它不停止VM将允许工作人员工作。

The only solution I have came up was to add: 我提出的唯一解决方案是添加:

IO.gets "Working... To finish hit <Enter>."

at the end... 在末尾...

I want to build an escript that after running will not halt the Erlang VM just like: 我想构建一个escript ,在运行后不会像以下那样停止Erlang VM:

elixir --no-halt -S mix run --eval 'MyApp.CLI.m
ain(["some-arg"])'

or 要么

mix run --no-halt --eval 'MyApp.CLI.m
ain(["some-arg1,some-arg2"])'

Is there a way to do this with escript? 有没有办法用escript做到这一点?

Or should I use a different solution to pack and distribute my program that is actually more like a server/daemon than a command line tool? 或者我应该使用不同的解决方案来打包和分发我的程序,实际上更像是服务器/守护程序而不是命令行工具?

A typical approach to packaging such systems is an OTP release. 包装此类系统的典型方法是OTP版本。 You can use exrm for that. 你可以使用exrm

If for some reasons, you still want to use escript, you can just call :timer.sleep(:infinity) after you start all the applications and processes. 如果由于某些原因,您仍然想使用escript,则可以在启动所有应用程序和进程后调用:timer.sleep(:infinity)

NOTE : Starting from Elixir 1.9 注意 :从Elixir 1.9开始

We can use System.no_halt(true) to allow script to never stop. 我们可以使用System.no_halt(true)来允许脚本永不停止。

Here is simple script example: 这是一个简单的脚本示例:

defmodule Mix.Tasks.NoHalt do
  use Mix.Task

  def run(_) do
    System.no_halt(true)
    IO.puts("Never die!")
  end
end

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

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