cannot start empty OTP application from command line - erlang

I have created an OTP application skeleton with rebar:
$ rebar create-app appid=test
then I compiled it with rebar compile, and when I run
$ erl -pa ebin -s test
I get this error
{"init terminating in do_boot",{undef,[{test,start,[],[]},{init,start_it,1,[]},{init,start_em,1,[]}]}}
but if I call start from the shell it works:
$ erl -pa ebin
Erlang R15B01 (erts-5.9.1) [source] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.1 (abort with ^G)
1> application:start(test).
ok
How can I start the application from the OS command line?
EDIT:
I figured that I needed to run
$ erl -pa ebin -s application start test
now I am not getting any errors, but the app is still not getting started...

erl -pa ebin/ -eval "application:start(test)"
Since the start function in test_app.erl has arity 2 it is not possible to invoke it directly using the erl switch -s (or -run), only arity 0 or 1 are possible to invoke with those switches (see http://erlang.org/doc/man/erl.html).
You could add a wrapper function that in turn calls the start/2, but I think the -eval is more elegant.

The -s flag assumes a list of arguments when one or more arguments are presented. So what $ erl -pa ebin -s application start test would do is calling application:start([test]) which would not work as expected.
Here is a workaround (might not be the best solution):
Create a source file src/test_init.erl with the following content:
-module(test_init).
-compile(export_all).
init() ->
application:start(test).
Then:
$ rebar compile
$ erl -pa ebin -s test_init init
Now the test application should be running:)

Related

Cannot use command line Elixir mix (macOS High Sierra)

When i run command elixir -v or mix, it will return:
{"init terminating in do_boot",{undef,[{elixir,start_cli,[],[]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
Crash dump is being written to: erl_crash.dump...done
init terminating in do_boot ()
Some details:
$ brew list
elixir erlang ...
$ erl
Erlang/OTP 19 [erts-8.0] [source-6dc93c1] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V8.0 (abort with ^G)
1>
$ elixir
Usage: elixir [options] [.exs file] [data]
-e COMMAND Evaluates the given command (*)
-r FILE Requires the given files/patterns (*)
...
I have used brew uninstall, reinstall, but nothing change. Please help me fix it!
erl without any arguments will run Erlang shell. But if you want to run Elixir shell you should try running iex (interactive elixir). elixir is used to run programs you already coded, like elixir simple.exs
Or it might be that your Erlang version is little out of date. Try running brew update.

How to run erlang (rebar build) application

I am new to Erlang world and currently can't figure out how to start my dummy erlang application. Probably, I am just missing something... So, I created an application with rebar (rebar create-app appid=dummys).
Currently I have
rebar.config
src/dummys.app.src
src/dummys_app.erl
src/dummys_sup.erl
I have found that in order to run an application during a development it is better to create an additional start method which should call application:start(module).
I added some basic logging to my start methods..
start() ->
error_logger:info_msg("Starting app(dev)..~n"),
application:start(dummys_app).
start(_StartType, _StartArgs) ->
error_logger:info_msg("Starting app..~n"),
dummys_sup:start_link().
If I try
erl -noshell -pa ebin -s application start dummys
erl -noshell -pa ebin -s application start dummys_app
there are no output..
If I try
erl -noshell -pa ebin -s dummys start
erl crashes with an error..
If I try
erl -noshell -pa ebin -s dummys_app start
it outputs just "Starting app(dev).." and that's all. But I also expect to see "Starting app.."
What I am missing or doing wrong??
=============
And another question: How to add a new module to my dummy application correctly? For example I have an additional module called "*dummys_cool*" which has a "start" method. How to tell my application to run that "dummys_cool#start" method?
Thank you!
For quick development, if you just want to ensure your appliction can start, start a shell, then start the application:
erl -pa ebin
1> dummys_app:start().
That will give you a clean indication of what is wrong and right without the shell bombing out after.
Since you're making an application to run, rather than just a library to share, you'll want to make a release. Rebar can get you most of the way there:
mkdir rel
cd rel
rebar create-node nodeid=dummysnode
After you've compiled your application, you can create a release:
rebar generate
This will build a portable release which includes all the required libraries and even the erlang runtime system. This is put by default in the rel/ directory; in your case rel/dummys.
Within that directory there will be a control script that you can use to start, stop, and attach to the application:
rel/dummys/bin/dummys start
rel/dummys/bin/dummys stop
rel/dummys/bin/dummys start
rel/dummys/bin/dummys attach
Have a look at your dummys.app.src file. The meaning of all the directives is explained in the 'app' manpage, but the one I suspect is missing here is mod, which indicates the name of your application callback module. So make sure that this line is present:
{mod, {dummys_app, []}}
The empty list in there will be passed as the StartArgs argument to dummys_app:start/2.
To make a new module start along with your application, add it to the supervision tree in dummys_sup:init. This function should look something like:
init(_) ->
{ok, {{one_for_one, 10, 10},
[{dummys_cool, {dummys_cool, start_link, []},
permanent, brutal_kill, worker, [dummys_cool]}]}.
This is described in the 'supervisor' manpage, but basically this means that on startup, this supervisor will start one child process. dummys_cool:start_link() will be called, and that function is expected to spawn a new process, link to it, and return its process id. If you need more processes, just add more child specifications to the list.
erl -noshell -pa ebin -s application start dummys
The code above will not work because application:start([dummys]) will be called.
You can take a reference of the Erlang documentation for details.
For your case,
erl -noshell -pa ebin -s dummys
I ran into this problem, and this was the first answer on Google.
If you are using rebar3, the standard configuration will have a shell command that compiles your project and opens a shell:
$ rebar3 shell
===> Analyzing applications...
===> Compiling myapp
Erlang/OTP 21 [erts-10.2.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
Eshell V10.2.4 (abort with ^G)
1> ===> Booted myapp

start common node in erlang shell

Facing problem with slave:start/2 in lion OS, so I have to try another direction "start common node with os:cmd automatically" in erlang shell.
One way is by command directly, and another way is through bash file. But both way faces the same problem. If can't start automatically, I have to start node manually everytime.
Eshell V5.9 (abort with ^G)
(emacs#yus-iMac.local)1> os:cmd("erl -name abcd").
"Eshell V5.9 (abort with ^G)\n(abcd#yus-iMac.local)1> *** Terminating erlang ('abcd#yus-iMac.local')\n"
(emacs#yus-iMac.local)3> os:cmd("~/Documents/Project/node_start").
"Eshell V5.9 (abort with ^G)\n(abc#yus-iMac.local)1> *** Terminating erlang ('abc#yus-iMac.local')\n"
The bash file content is as follows:
#/bin/sh
erl -name abc
Not that I'm really fond of this approach, but to make the above work you should pass the "detached" parameter to the 'erl' command:
erl -name foo
1> os:cmd("erl -name bar -detached").
[]
2> net_adm:ping('bar#pigeon.local').
pong
3> nodes().
['bar#pigeon.local']
Reading from the doc:
-detached
Starts the Erlang runtime system detached from the system console. Useful for running daemons and backgrounds processes. Implies
-noinput.

Is there way to run a file with instruction in the erlang shell?

I have a file .erlang in the current dir I run erl shell that compiles a few modules.
Is there way to re run this file from the Erlang shell.
I run erl I get the shell 1> and from there run like run_file(".erlang") and this file execute the instructions in the current shell and load the env.
The file looks like
compile:file(file1).
compile:file(file2).
compile:file(file3).
...
You're looking for file:eval/1.
Given a .erlang with this content:
io:format("Hello world!~n").
You get:
Eshell V5.7.5 (abort with ^G)
1> file:eval(".erlang").
Hello, world!
ok
You might want to look into escript.

How do I install LFE on Ubuntu Karmic?

Erlang was already installed:
$dpkg -l|grep erlang
ii erlang 1:13.b.3-dfsg-2ubuntu2 Concurrent, real-time, distributed function
ii erlang-appmon 1:13.b.3-dfsg-2ubuntu2 Erlang/OTP application monitor
ii erlang-asn1 1:13.b.3-dfsg-2ubuntu2 Erlang/OTP modules for ASN.1 support
ii erlang-base 1:13.b.3-dfsg-2ubuntu2 Erlang/OTP virtual machine and base applica
ii erlang-common-test 1:13.b.3-dfsg-2ubuntu2 Erlang/OTP application for automated testin
ii erlang-debugger 1:13.b.3-dfsg-2ubuntu2 Erlang/OTP application for debugging and te
ii erlang-dev 1:13.b.3-dfsg-2ubuntu2 Erlang/OTP development libraries and header
[... many more]
Erlang seems to work:
$ erl
Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.4 (abort with ^G)
1>
I downloaded lfe from github and checked out 0.5.2:
git clone http://github.com/rvirding/lfe.git
cd lfe
git checkout -b local0.5.2 e207eb2cad
$ configure
configure: command not found
$ make
mkdir -p ebin
erlc -I include -o ebin -W0 -Ddebug +debug_info src/*.erl
#erl -I -pa ebin -noshell -eval -noshell -run edoc file src/leex.erl -run init stop
#erl -I -pa ebin -noshell -eval -noshell -run edoc_run application "'Leex'" '"."' '[no_packages]'
#mv src/*.html doc/
Must be something stupid i missed :o
$ sudo make install
make: *** No rule to make target `install'. Stop.
$ erl -noshell -noinput -s lfe_boot start
{"init terminating in do_boot",{undef,[{lfe_boot,start,[]},{init,start_it,1},{init,start_em,1}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Is there an example how I would create a hello world source file and compile and run it?
No, there is nothing you missed. The Makefile in LFE is "less than perfect" and should be ignored, it will be improved upon in the next release. To compensate all the needed files have already compiled and the .beam files are in the ebin directory. As it is not part of OTP I don't think it should ever install there.
The easiest way to handle this to create a private erlang library directory and point the environment variable ERL_LIBS to it. Then just drop the whole LFE directory there. When erlang starts the code server will automatically add the lfe/ebin directories into the path and the .beam files there will automagically be found and loaded. This will work with any package that contains an ebin directory. This also works on Windows. So:
Make an libs directory, say ~/erlang/lib
Set the environment variable ERL_LIBS, export ERL_LIBS=~/erlang/lib
Put the whole LFE directory there
When you start erlang you will then see /Users/rv/erlang/lib/lfe/ebin (or wherever you have it) in the code path (code:get_path()). You will then also be able to start the LFE shell directly with
erl -noshell -noinput -s lfe_boot start
There will be an lfe and an lfe.bat which does this included as well in the future.
As with erlang any text editor will work to edit LFE. For emacs there is an LFE mode which is still rather basic but works. You cannot yet run LFE in a window. Soon. The best way to include this is to put the following in your .emacs file:
;; LFE mode.
(setq load-path (cons "/Users/rv/erlang/lib/lfe/emacs" load-path))
(require 'lfe-start)
There are some example files in lfe/examples, all should work. In lfe/test/visual there is a bunch of my test files which have been included as example files. To compile an LFE file from the normal erlang shell do
lfe_comp:file("foo").
l(foo). %No autloload here, do this to ensure loading
while from the LFE shell do:
(c '"foo") ;This will autoload
There is a bunch of documentation in lfe/docs which is quite accurate but the user_guide.txt needs to be extended. There is also a Google group for LFE at
http://groups.google.se/group/lisp-flavoured-erlang
which contains some interesting discussions and people have written quite a lot in the github LFE wiki.
That's about it I think. contact me if/when you have more questions.

Resources