Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 546
Szymon Jeż Avatar asked Mar 16 '23 04:03

Szymon Jeż


2 Answers

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.

like image 163
sasajuric Avatar answered Apr 27 '23 02:04

sasajuric


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
like image 22
Virviil Avatar answered Apr 27 '23 01:04

Virviil