4

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.

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:

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?

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?

Szymon Jeż
  • 8,273
  • 4
  • 42
  • 60

2 Answers2

7

A typical approach to packaging such systems is an OTP release. You can use exrm for that.

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.

sasajuric
  • 5,949
  • 1
  • 22
  • 18
  • The `sleep` solution looks better than the `gets`. Thank you. Do you think that having the ability to pass an `--no-halt` (and also `--detached` or any other option) to the elixir command that runs the program inside the escript, would make sense/would be useful? – Szymon Jeż May 06 '15 at 12:45
  • As I said, I think that using OTP releases is more idiomatic for such cases. I'm not sure that `--no-halt` can work with escripts, because with escripts Erlang VM simply runs the main code and then stops. AFAIK, all arguments are passed directly to the script, so I don't see a clean way for doing parameters preprocessing. Again, you should really consider using OTP releases for your case. – sasajuric May 06 '15 at 13:18
  • By "passing options to the elixir command that runs the program inside the escript" I have meant doing it at build/compile time of the escript. For sure I will checkout OTP releases, but I also have the feeling that having the option to compile the escript with `--no-halt` etc. flags could be useful. – Szymon Jeż May 06 '15 at 13:30
  • The only way I can think of for that to work is to inject the timer sleep call in the generated script. This is something you can just as easily do yourself, so I personally don't see particular benefits. But maybe you should consider discussing this on elixir-lang-core to get opinions of others. – sasajuric May 06 '15 at 13:54
2

NOTE: Starting from Elixir 1.9

We can use System.no_halt(true) to allow script to never stop.

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
Virviil
  • 622
  • 5
  • 14