# Run function on startup

**URL:** <https://forum.elixirforum.com/t/run-function-on-startup/30289>\
**Category:** Questions\
**Tags:** startup\
**Created:** [April 1, 2020, 3:34pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289 "2020-04-01T15:34:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroenhouben](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeroenhouben/32/1901_2.png) [@jeroenhouben](https://forum.elixirforum.com/u/jeroenhouben)\
**Post date:** [April 1, 2020, 3:34pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289/1 "2020-04-01T15:34:20Z")

</div>

Simple question I could not find an answer for…

I want to perform a HTTP request once at startup of my app and log the output. Phoenix app in my case but the question is general really. I don’t need a whole genserver and all, just a function to run… somewhere

Where do I put this code?

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [April 1, 2020, 3:39pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289/2 "2020-04-01T15:39:57Z")

</div>

Add one of task in your supervisor children list. You can use [`Task.child_spec/1`](https://hexdocs.pm/elixir/Task.html#child_spec/1) for that. This is what I am using for such jobs like Slack notifications or systemd notifications.

---

<div class="post-metadata">

**Author:** ![entone](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/entone/32/18061_2.png) [@entone](https://forum.elixirforum.com/u/entone)\
**Post date:** [April 1, 2020, 4:00pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289/3 "2020-04-01T16:00:16Z")

</div>

You can also use the `:ignore` return value in a GenServer init callback in your supervision tree, that will run your init callback and then kill/ignore the process after it’s run.

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [April 1, 2020, 6:57pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289/4 "2020-04-01T18:57:25Z")

</div>

I strongly disrecommend using GenServer with ignore for a one-off, linear sequence of events. The semantic idea of ignore, is that “you’re starting a stateful system that checks in and decides it doesn’t need to exist, which is not an error so you can’t use `{:stop, reason}`”.

---

<div class="post-metadata">

**Author:** ![entone](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/entone/32/18061_2.png) [@entone](https://forum.elixirforum.com/u/entone)\
**Post date:** [April 1, 2020, 7:47pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289/5 "2020-04-01T19:47:28Z")

</div>

You could just as easily return `{:stop, reason}` if you run into an error or `:ignore`, but maybe I’m not following exactly what you’re getting at?

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [April 1, 2020, 8:01pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289/6 "2020-04-01T20:01:02Z")

</div>

In such case I would prefer to use `proc_lib` directly.

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [April 1, 2020, 8:07pm UTC](https://forum.elixirforum.com/t/run-function-on-startup/30289/7 "2020-04-01T20:07:37Z")

</div>

```elixir
defmodule Foo do 

  def go do
    GenServer.start_link( __MODULE__ , :ok)
  end

  def init(_) do
    :ignore
  end

end

defmodule Bar do 

  def go do
    GenServer.start_link( __MODULE__ , :ok)
  end

  def init(_) do
    {:stop, :reason}
  end

end

defmodule Baz do 

  def go do
    GenServer.start_link( __MODULE__ , :ok)
  end

  def init(_) do
    {:stop, :normal}
  end

end

defmodule Quux do

  def go do
    Task.start_link(fn -> :do_something end)
  end

end

```

try them all in the console to get a feel of what the supervisors see.
