bvobart

bvobart

How do I exit the BEAM VM with exit code 0 on top-level supervisor auto-shutdown?

Hi Elixir community :blush:

I’m having some trouble getting the BEAM VM to exit with exit code 0 upon auto-shutdown of the top-level supervisor.

For context, I’m writing an application that runs a cron job in cloud environment in a Docker container. So periodically, the cloud environment starts the container, which starts up my application, which runs a function to do some stuff. After that function exits (without raising), I want my application to exit and subsequently, I want the container to exit with exit code 0, so that the cloud environment knows that the run was successful.

Here’s my application module:

defmodule OpiniLead.Scrape.Application do
  @moduledoc """
  The application module for `OpiniLead.Scrape`.
  """
  use Task, significant: true
  use Application

  def start_link(_) do
    scrapers = Application.fetch_env!(:opinilead, :scrapers)
    Task.start_link(OpiniLead.Scrape, :run, [scrapers])
  end

  def start(_type, _args) do
    Supervisor.start_link([__MODULE__],
      strategy: :one_for_one,
      auto_shutdown: :all_significant,
      name: OpiniLead.Scrape.Supervisor
    )
  end
end

When I start this application, the task runs successfully and the supervisor exits with reason :shutdown, as expected. However, the BEAM VM seems to interpret this as a crash and exits with error code 1:

{"message":"Application opinilead exited: shutdown","@timestamp":"2025-03-03T10:56:46.693Z","ecs.version":"8.11.0","log.level":"notice","log.logger":"application_controller","log.origin":{"function":"info_exited/3","file.line":2125,"file.name":"application_controller.erl"}}
Kernel pid terminated (application_controller) ("{application_terminated,opinilead,shutdown}")

Crash dump is being written to: erl_crash.dump...done

How do I let the BEAM VM interpret :shutdown as a normal / successful exit reason so it exits without a crash (and with exit code 0)? Or should I use an entirely different approach to release a Docker image that just runs a function until completion and then exits?

Note: I’m currently using mix releases to build my application into an executable for the Docker image, which is then started with bin/opinilead start. I also have a few tests which call Application.start(:opinilead) and then verify that the supervisor starts, runs and exits, which prevents me from straight-up calling System.stop(0) in the application’s stop/1 callback.

Marked As Solved

bvobart

bvobart

Hmmmm okay yeah I was afraid of that. It still feels wrong to use System.stop in an application, it might be a good indication that applications are not the right abstraction for releasing a Docker image that runs such one-off tasks.

I found a different solution though, one that might be a bit cleaner. Simply put: use eval instead of start.

My application module now looks as follows:

defmodule OpiniLead.Scrape.Application do
  @moduledoc """
  A pseudo-application module for `OpiniLead.Scrape`.
  Usually, an Application's `start/2` callback would start a supervisor that manages a permanently
  running system. However, in this case, we want to start a one-off task, so we define a `run` function
  that can be 'eval'ed to run the scraping as a one-off task,
  e.g. `mix run -e 'OpiniLead.Scrape.Application.run()'`.
  """

  def run do
    Application.ensure_all_started(:opinilead)
    Application.fetch_env!(:opinilead, :scrapers) |> OpiniLead.Scrape.run()
  end
end

I’ve also removed the mod key from the application callback in my mix.exs file.

I can then use mix run -e 'OpiniLead.Scrape.Application.run()' to run my one-off task in development mode. For the released Docker image, I set the command to bin/opinilead eval 'OpiniLead.Scrape.Application.run()'

With that, my app runs properly, exits with exit code 0 on success and crashes neatly when fatal errors are raised.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I would use the System.stop function to shut down the node on completion.

LostKobrakai

LostKobrakai

Yeah, even a normal shutdown for a top level supervisor is an abnormal behaviour as a permanent application is not meant to have its top level supervisor exit. Even a :transient application only allows for :normal exit reason, not :shutdown.

LostKobrakai

LostKobrakai

Application.ensure_all_started(:opinilead) should do all fo that for you.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New

We're in Beta

About us Mission Statement