Running Erlang Code From The Command Line

From Erlang Community

Revision as of 02:49, 10 October 2006 by Cyberlync (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)

[edit] Overview

Running erlang code as a command line application can prove to be pretty difficult if you don't know what you are doing. Fortunatly, its not that hard once someone explains it to you.

[edit] Running From The Command Line

This can be done with the '-s' or '-run' flag:

$>erl -run Mod [Fun [Args]]:
Passes the -run flag to the init:boot() routine.

$>erl -s Mod [Fun [Args]]:
 Passes the -s flag to the init:boot() routine.

-run Module [Function [Args]] evaluates the specified function during system initialization. The 'function' defaults to 'start' and Args to [] if they are not specified. If the function call ends abnormally, the Erlang runtime system stops with an error message. Any arguments after -run are used as arguments to Erlang functions. All arguments are passed as strings. For example:

$>erl -run foo -run foo bar -run foo bar baz 1 2

This starts the Erlang runtime system and then evaluates the following Erlang functions:

foo:start()
foo:bar()
foo:bar(["baz", "1", "2"]).

The functions are executed sequentially in the initialization process, which then terminates normally and passes control to the user. This means that a -run call which does not terminate will block further processing; to avoid this, use some variant of spawn in such cases.

The '-s' option is very similar to '-run'. The only large difference is that all arguments are passed as atoms instead of strings.

$>erl -s foo -s foo bar -s foo bar baz 1 2

This starts the Erlang runtime system and then evaluates the following Erlang functions:

foo:start()
foo:bar()
foo:bar([baz, '1', '2']).

The functions are executed sequentially in the initialization process, which then terminates normally and passes control to the user. This means that a -s call which does not terminate will block further processing; to avoid this, use some variant of spawn in such cases.

Due to the limited length of atoms, it is recommended that -run be used instead.

If pou don't want the shell You can also add -noshell. If you are running a simple one off script you usually want to run the script and then shut the erlang node down. You can do this by adding -run init stop to the end of your command line. For example,

$>erl -pa ./ -run mymodule myfunc -run init stop -noshell

This will do what you want 99% of the time. It will run mymodule:myfunc(), then it will run init:stop() without ever starting a shell. There is one big caveat here. If your main thread starts worker threads and then exits init:stop will be run and it will kill everything that hasn't already finished. There are a couple of solutions to this problem. One is to make sure your main process (the one started in mymodule:myfunc) doesn't exit until all work is done. The other option is not to add init stop to your command line your application call init:stop when it is finished with everything it needs to do.

Erlang/OTP Projects
Personal tools