How to run an Elixir application?

mix run does run your app. It's just that when you simply put IO.puts "something" in a file that line is only evaluated in compile-time, it does nothing at runtime. If you want something to get started when you start your app you need to specify that in your mix.exs.

Usually you want a top-level Application that will get started. To achieve that add a mod option to your mix.exs:

def application do
  [
    # this is the name of any module implementing the Application behaviour
    mod: {NewMix, []},
    applications: [:logger]
  ]
end

And then in that module you need to implement a callback that will be called on application start:

defmodule NewMix do
  use Application

  def start(_type, _args) do
    IO.puts "starting"
    # some more stuff
  end
end

The start callback should actually setup your top-level process or supervision tree root but in this case you will already see that it is called every time you use mix run, although followed by an error.

def start(_type, _args) do
  IO.puts "starting"
  Task.start(fn -> :timer.sleep(1000); IO.puts("done sleeping") end)
end

In this case we are starting a simple process in our callback that just sleeps for one second and then outputs something - this is enough to satisfy the API of the start callback but we don't see "done sleeping". The reason for this is that by default mix run will exit once that callback has finished executing. In order for that not to happen you need to use mix run --no-halt - in this case the VM will not be stopped.

Another useful way of starting your application is iex -S mix - this will behave in a similar way to mix run --no-halt but also open up an iex shell where you can interact with your code and your running application.


You can run tasks by importing Mix.Task into your module instead of mix run.

I think this is what you're looking for.

On top of that, instead of mix <task.run>, you can simply run mix to run the default task. Simply add default_task: "bot.run" into the list of def project do [..] end in mix.exs. Refer here.

Tags:

Elixir