# CLI Application with OTP - application:start/2 doesn't work

**URL:** <https://erlangforums.com/t/cli-application-with-otp-application-start-2-doesnt-work/3485>\
**Category:** Questions / Help\
**Tags:** erlang, erlang-otp\
**Created:** [April 12, 2024, 6:35am UTC](https://erlangforums.com/t/cli-application-with-otp-application-start-2-doesnt-work/3485 "2024-04-12T06:35:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![acema](https://erlangforums.com/letter_avatar_proxy/v4/letter/a/cc9497/32.png) [@acema](https://erlangforums.com/u/acema)\
**Post date:** [April 12, 2024, 6:35am UTC](https://erlangforums.com/t/cli-application-with-otp-application-start-2-doesnt-work/3485/1 "2024-04-12T06:35:58Z")

</div>

I completely rely on the OTP principles. That’s why I use applications, gen\_server and stuff overall and it works for me very well. There is only one issue with CLI applications:

If all code is started with applications and application:start/2 is supposed to start a supervisor than it doesn’t work in this case. One application is meant to be the main controller of the CLI application that only calls server functions.

Following solutions don’t work:

- run the controller in `start/2` and stop the whole program/release(?) with `init:stop()`. This will raise `bad return value ok from almmain_app:start(normal, [])`
- manually patch the boot script to eval code to start the controller in a separate module
- use rebar3 to provide an additional entry point. At least I couldn’t find any configuration option

I am curious if OTP is only used for pure server applications.

---

<div class="post-metadata">

**Author:** ![mmin](https://erlangforums.com/user_avatar/erlangforums.com/mmin/32/2193_2.png) [@mmin](https://erlangforums.com/u/mmin)\
**Post date:** [April 12, 2024, 8:28am UTC](https://erlangforums.com/t/cli-application-with-otp-application-start-2-doesnt-work/3485/2 "2024-04-12T08:28:44Z")

</div>

If you want to do CLI applications, try [escripts](https://www.erlang.org/doc/man/escript) (with [argparse](https://www.erlang.org/doc/man/argparse#) for processing arguments).

---

<div class="post-metadata">

**Author:** ![xsteve](https://erlangforums.com/letter_avatar_proxy/v4/letter/x/8dc957/32.png) [@xsteve](https://erlangforums.com/u/xsteve)\
**Post date:** [April 12, 2024, 8:28am UTC](https://erlangforums.com/t/cli-application-with-otp-application-start-2-doesnt-work/3485/3 "2024-04-12T08:28:39Z")

</div>

Hallo acema,

I am also very new to elixir and CLI applications.

This is what I found on how to run an application and provide a needed Supervisor:

defmodule Hello do  
use Application  
def start(\_type, \_args) do  
IO.puts(“Hello elixir”)  
Supervisor.start\_link(, strategy: :one\_for\_one)  
end  
end

---

<div class="post-metadata">

**Author:** ![mikpe](https://erlangforums.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@mikpe](https://erlangforums.com/u/mikpe)\
**Post date:** [April 12, 2024, 4:46pm UTC](https://erlangforums.com/t/cli-application-with-otp-application-start-2-doesnt-work/3485/4 "2024-04-12T16:46:25Z")

</div>

Erlang is fine-ish for CLI tools, I’ve written a number of them. You can use escripts or just shell wrappers that run the code with `erl -noshell -pa ... -run ...`. A few years ago I had to work around `escript` crash dumps going to the wrong output (`stdout` instead of `stderr`) but that’s fixed now.  
As to OTP principles, I use gen\_servers where apppropriate but not supervisors, and therefore not full-on stateful applications (they’re all library applications). A compiler say has little use for supervising the process holding lexer state.

---

<div class="post-metadata">

**Author:** ![acema](https://erlangforums.com/letter_avatar_proxy/v4/letter/a/cc9497/32.png) [@acema](https://erlangforums.com/u/acema)\
**Post date:** [September 27, 2024, 8:27am UTC](https://erlangforums.com/t/cli-application-with-otp-application-start-2-doesnt-work/3485/5 "2024-09-27T08:27:47Z")

</div>

The problem was wrong handling of OTP processes. You are discouraged to start a gen\_server in a Supervisor with :start\_link/2 and expect it will be closed properly.
