Closed
Description
I'm trying to create a simple hello world program...
-module(myapp_app).
-behaviour(application).
%% Application callbacks
-export([start/2, stop/1]).
%%====================================================================
%% API
%%====================================================================
start(_StartType, _StartArgs) ->
io:format("hello world~n"),
myapp_sup:start_link().
%%--------------------------------------------------------------------
stop(_State) ->
ok.
These are the steps...
~$ rebar3 new app myapp
===> Writing myapp/src/myapp_app.erl
===> Writing myapp/src/myapp_sup.erl
===> Writing myapp/src/myapp.app.src
===> Writing myapp/rebar.config
===> Writing myapp/.gitignore
===> Writing myapp/LICENSE
===> Writing myapp/README.md
~$ rebar3 escriptize
===> Verifying dependencies...
===> Compiling myapp
===> Building escript...
~$ _build/default/bin/myapp
escript: exception error: undefined function myapp:main/1
in function escript:run/2 (escript.erl, line 759)
in call from escript:start/1 (escript.erl, line 277)
in call from init:start_em/1
in call from init:do_boot/3
...
Why doesn't this work what do I have to do so it launches my application?
Using shell does work...
rebar3 shell --apps myapp
===> Verifying dependencies...
===> Compiling myapp
Erlang/OTP 20 [erts-9.0.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:0] [kernel-poll:false]
Eshell V9.0.4 (abort with ^G)
1> ===> The rebar3 shell is a development tool; to deploy applications in production, consider using releases (http://www.rebar3.org/docs/releases)
hello world
===> Booted myapp
I want to run it the application in the background not use a shell to attach. How do I create the escript to launch it or do I have to do a release? I apologize if this is a newbie question, but I feel like this should be the easiest thing to do but I cannot figure it out :/
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
fenollp commentedon Apr 6, 2018
What you seem to be looking for is how to create a simple CLI app with Erlang.
rebar3 escriptize
is the right way to go however you got that error message:meaning you need to create a module called
myapp
which has to export amain
function taking 1 argument. You should put your hello world code in there.Here's more info on Erlang CLI apps (they're called
escript
s).lfmunoz commentedon Apr 6, 2018
@fenollp thank you, that got me a step further, but I can't get the application to start. I tried the following
myapp_up:start_link() appears to be doing nothing. The above is just a simple example but I am actually running a tcp service that suppose to accept connections.
I think rebar3 escriptize doesn't package the full vm runtime, so I tried including that in rebar.config
That doesn't work, gets error when trying to "process kernel"
I'm gonna stick with releases unless someone can give me a clue on how to get this working.
fenollp commentedon Apr 8, 2018
There is never any VM runtime packaged into an escript. You can take a look at what’s inside an escript with
unzip -l my.escript
. In my experience you rarely need to ever use escript_incl_apps, you should probably be setting these app dependencies in your .app.src instead.Note that if your app talks with the web it may already need to have
inets
there.ferd commentedon Apr 20, 2018
Here's a full example using
rebar3 escriptize
:So what does this require? First of all, let's take a look at the rebar.config file:
escript_main_app
set to the application name I want to use. (here,hello
)hello
) that exports a functionmain/1
applications
tuple of your .app.src file (in src/)With these two things we can build an escript. The escript, as @fenollp said, ships without the Erlang VM and requires it to be installed. It makes Erlang become a scripting language the same way Python or Ruby would be, in some ways. It is still compiled and bundles its own code, but depends on a runtime environment being present.
If you want to boot your application in the escript, then calling
application:ensure_all_started(YourApp)
is likely the best way to go, and atimer:sleep(infinity).
will help things keep running.Let us know if you need more assistance.