Start children of dynamic supervisor after it's started



Having a list of strings where every string represents an argument for a child, how can I start them all after DynamicSupervisor.init? Or should I use plain Supervisor? Or am I doing it wrong at all? Thanks

If I add lines |> Enum.each(&add/1) after DynamicSupervisor.init, it doesn’t seem to work and as I understand it’s a bad practice to do this inside init.


    children = [
      {Pbot.Stash, []},
      {Pbot.SymbolSupervisor, ["one", "two", "three"]}


defmodule Pbot.SymbolSupervisor do
  use DynamicSupervisor

  def start_link(lines) do
    DynamicSupervisor.start_link(__MODULE__, lines)

  def init(lines) do
    DynamicSupervisor.init(strategy: :one_for_one)
    # lines |> Enum.each(&add/1)

  def add(line) do
    {:ok, _pid} = DynamicSupervisor.start_child(__MODULE__,  {Pbot.M, line})


04:18:17.411 [info]  Application pbot exited: Pbot.Application.start(:normal, []) returned an error: shutdown: failed to start child: Pbot.SymbolSupervisor
    ** (EXIT) exited in:, {:start_child, {{Pbot.M, :start_link, [[%Pbot.Pair{...}, %Pbot.Pair{...}]]}, :transient, 5000, :worker, [Pbot.M]}}, :infinity)
        ** (EXIT) process attempted to call itself


You probably just want a normal supervisor if you know all the processes that are going to exist at startup.


Two problems with your code:

  • You need to give a :name option to the DymanicSupervisor.start_link call as you assume it has been set to the module name.
  • You are making a synchronous call to yourself as the DynamicSupervisor.start_child call is made from inside the supervisor process. If this had been allowed the supervisor process would hang. This what the 2nd and 3rd error lines are about. The 1st error line comes from this supervisor’s supervisor exiting because its children crashed at startup.

And you have the issue which @blatyo mentioned: do you really want/need a normal supervisor here as you know all child processes? The only difference here would be the termination order of the children.


Thanks. The name was there at first :slight_smile: I figured out that I could start children of DS from another process like a genserver process. Then ended up doing the following

  defp get_children do
    |> ->
      Supervisor.child_spec({Pbot.M, line}, id:

and then

def start do
  lines = get_children()
  Supervisor.start_link(lines ++ children, opts)

id goes to child’s start link func GenServer.start_link(__MODULE__, ..., name: id)