mix run launches your application. Just when you just put IO.puts "something" in a file, this line is evaluated only at compile time, it does nothing at runtime. If you want something to start when the application starts, you need to specify it in mix.exs .
Usually you need an entry-level Application that will be launched. To do this, add the mod option to mix.exs :
def application do [
And then in this module you need to implement a callback that will be called when the application starts:
defmodule NewMix do use Application def start(_type, _args) do IO.puts "starting"
The start callback should actually set up your top-level root directory or control tree, but in this case you will already see that it is called every time you use mix run , although it is 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 start a simple process in our callback that just sleeps for one second and then outputs something - this is enough to satisfy the start callback API, but we donβt see "done sleeping" . The reason for this is that by default mix run will exit after the callback completes. To avoid this, you need to use mix run --no-halt - in this case, the VM will not be stopped.
Another useful way to launch your application is iex -S mix - this will behave similarly to mix run --no-halt , but also open the iex shell where you can interact with your code and the running application.
PaweΕ Obrok Jun 07 '15 at 0:29 2015-06-07 00:29
source share