Run one thing & quit

I want to write a utility that launches, does one thing (which might take quite a while), then exits, without logging an error and returning 0 to the OS. I want the “one thing” to be supervised such that if it exits abnormally, it is run again. I want to deploy the normal way via using distillery to create a package which is complete.

I’m having trouble figuring out the options between escript vs my deploy needs, regular application vs the exit reporting an error.

1 Like

I just hadn’t looked in all the right places: System.halt(0) will get me what I want, although it seems like a rude way to do it, so definitely open to better suggestions.

1 Like

The proper way to shut down if OTP is fully up is via :init.stop(). It lets everything properly shut down and terminate before bringing the VM down.

1 Like

Don’t use destillery, it’s meant for demonized stuff. Do an escript which calls the function which runs the CMD recursively on failure.

2 Likes

As I’m now on a computer instead of a mobile, I can give you better example:

def loop do
  case System.cmd("./foo.sh", []) do
    {_, 0} -> nil
    {_, _} ->
      IO.puts "."
      loop()
  end
end

loop

while foo.sh has this content:

#!/usr/bin/env bash

exit $((RANDOM % 10))

Of course, this is overly simplified, as it will not output anything from the cmd to run. It just prints a dot and a newline for every failing run.

If you need proper forwarding of the scripts output to stdout and stderr, you need to use the Port module or one of the available wrapper packages (which I only know that they exist, but I can’t name them)

3 Likes

That covers, roughly, the re-try (although to be clear I am not running an external command, I’m running Elixir code that may take a long time). But it doesn’t cover getting me a self-contained package to deploy.

1 Like

How self contained does it needs to be?

With a bit of changes you can compile the code into an escript which does only depend on Erlang.

1 Like

It’s exactly the requirement to have Erlang pre-installed that I want to avoid.

1 Like

erlang is a required dependency for running Elixir.

1 Like

Why? Such a file were about 50 megabytes in size, plus the code you wrote on your own.if you have multiple of such self contained packages they would contain much identical stuff, which is shared when you just require erlang.

1 Like