starting erlang application with parameter - erlang

Is there a way to pass parameters to root supervisor of an application other than with config file and application:get_env/1? For instance, by command line?
I start my app as "erl -pa ebin -run appname", and then communicate with it by TCP/IP. TCP port on which it listens is set in ebin/appname.app, in env part. Now I'd like to be able to tell my app to forget that and listen on a port which I would give on command line (something like "erl -pa ebin -run appname -env [{port, 1234}]"). Is there a standardised pattern for that?
The problem is that I sometimes decide the app should start on another, non default port, for testing purposes, and changing the .app file every time is just pain in the ass.
Regards,
dijxtra

Yes. You can override the value of an environment variable via the command line, using:
erl -appname key value
And retrieving the parameter using:
application:get_env(appname, key).

Related

Erlang heartbeats

I am trying to create an application that can run on two different machines on the same network, and when one of the applications crahes, I want to use erlang heartbeat system to make it restart. How can I do this?
I've read the documentation, but have not figured out how to achieve this in practice.
Thanks
Did you specifically read http://erlang.org/doc/man/heart.html and try to follow the instructions there? In particular, you have to first set the environment variable HEART_COMMAND to the full command line to be used to restart your system.
To make this easier, you could use a launch script like this:
#!/bin/sh
erl -detached -heart -env HEART_COMMAND "$0 $#" -env HEART_BEAT_TIMEOUT 20 -sname mynode
In some environments (such as embedded systems) you might prefer a full OS reboot, and could simply run something like this:
#!/bin/sh
erl -detached -heart -env HEART_COMMAND "reboot" -env HEART_BEAT_TIMEOUT 20 -sname mynode

client on one node server on other node but on same host in erlang language

Here is the code iam confused when i was executing in Erlang shell so please explain me
In first terminal we set the terminal name as gandalf
erl -sname gandalf
(gandalf#localhost) 1> kvs:start().
In second terminal terminal name is bilbo
erl -sname bilbo
(bilbo#localhost) 1> rpc:call(gandalf#localhost, kvs,store, [weather, fine]).
But the problem is when i was executing the above code in respective terminals code was not getting executed so please explain me and i have one more doubt is it compulsory to set cookies??
Do you connect node before rpc:call?

ejabberd - Running escript from the same context as `ejabberdctl debug`

Part of setting up my ejabberd server includes running ejabberdctl debug and entering some commands. Instead, I would like to have a file that is executed, but have it run from the same context of the ejabberdctl debug REPL. How can I do this?
You can use the rpc:call command. The Node value is what you see at the debug prompt "name#host".
Also, make sure the escript sets the appropriate cookie value.
Let me clarify, if the debug environment looked like this:
(ejabberd#host)1>
You can create a escript file which looks like this. This updates the log level:
#!/usr/bin/env escript
%%! -sname script1 -setcookie cookie -hidden
main([Level]) ->
rpc:call('ejabberd#host', ejabberd_loglevel, set, [list_to_atom(Level)]).

How to see Erlang application Console which is running in backgroud?

I am using rebar for release build of erlang application, when I use start option to start the application it is running fine in background and It returns me the command prompt. I don't want to see all the background output, so I did not run using console option. But If I need any time what is going in background, to check the console due to any error, how do I get that running application's console?
I guess that you have made a release using Rebar and that you have started the node with the generated start script.
So the best way would be to use the start option 'attach':
./bin/mynode attach
It will connect to the shell through pipes so you will be in the actual node that are running so be careful with using Ctrl-c. (add the option "+Bi" to your vm.args file to restrict that..)
You can connect a remote shell to the node, provided it's been set up for distribution. Use the following command:
erl -sname rem -remsh node#host -setcookie the_cookie -hidden
Ctrl-G to enter JCL mode, then 'j' to list, then 'c' followed by a number to connect to the chosen job. See the eshell docs, specifically the JCL section.
Oh, or if by 'command prompt' you mean an OS shell rather than an Erlang shell, IIRC you need to start an Erlang node that is appropriately -name'd or -sname'd (whichever the node you want to connect to uses), then connect to that node ('r' in JCL mode), then connect to the job.

Run erlang application without terminal depending

I have erlang application: *.app file and some *.erl files. I compile all of them. In terminal i start erl and there application:start(my_application)., all ok, but if i closed terminal application close too. How can i run application without terminal depending?
Thank you.
You likely want to use the -noshell option to erl. The syntax is
erl -noshell -s Module Function Arguments
So in your case, this might be
erl -noshell -s application start my_application
This should allow you (for example if you are on Unix/Linux) to start your application as a background process and leave it running.
One useful variation is to also call the stop/0 function of the init module so that the Erlang environment will stop when it has finished running your function. This comes in handy if you want to run a simple one-use function and pipe the output to some other process.
So, for example, to pipe to more you could do
erl -noshell -s mymodule myfunction -s init stop | more
Finally, you might also be able to use the escript command to run your Erlang code as scripts rather than compiled code if it makes sense for your situation.
Hope that helps.
The proper way to handle this situation, is building a release containing your app and running the system as so called embedded one.
This release is going to be completely independent (it will hold erts and all the libs like, kernel, std, mnesia etc.).
On start, the new process will not be connected to shell process.
It will be OS process, so you can attach to it with pipes. All script are included in OTP.
Here is some info: http://www.erlang.org/doc/design_principles/release_structure.html
It may seem to be complicated, but tools like rebar do everything for you.

Resources