tags:

views:

387

answers:

5

To start my program I do the next sequence:

$ erl
> c(module1).
> c(module2).
> c(modulen).
modulen:start().

Is there any possibility to create script that allow me to launch my program ?

+1  A: 

You can compile the modules with erlc

erlc module1 module2 module3

You can also create a script to run you program

#!/usr/bin/escript

main(_) ->
    modulen:start().

Then in the console:

chmod +x start.erl
./start.erl
Emil Ivanov
+4  A: 

You could use a loader script that takes care of starting your application in an OTP fashion:

-module(XYZ_app).

-export([start/0]).

start() ->
    application:start(inets),
    application:start(XYZ).

You launch that script through a shell script. Beware of using escript if you plan on building daemons running at the OS boot time as they are tricky.

#!/bin/bash
erl -boot start_sasl -s XYZ_app start

Of course you need your XYZ.app file (just an example):

{application, tinycouch,
 [{description, "tinycouch"},
  {vsn, "0.1"},
  {modules, [
         tinycouch, tinycouch_app, tinycouch_sup,
         tinycouch_server, tinycouch_logger_h, tinycouch_utils,
         tinycouch_db, mod_tinycouch
        ]},
  {registered, [tinycouch
         ,tinycouch_server
         ,tinycouch_sup
           ]},
  {applications, [kernel, stdlib, sasl, inets]},
  {env, []},

  %% Application Start point
  {mod, {tinycouch_sup, []}}]}.

... and all your .erl files must have been compiled.

Note that if you intend to distribute your application (e.g. Debian repository etc), you probably should consider having a make file to compile and install your app.

As a final note: you could also go with a boot file (the ultimate OTP way) but I find those very constraining: a boot file ties your application to specific version release of Erlang (and other dependent applications). Your application might be able to run in various Erlang releases but you will need to have a separate build/release process for each "platform release" you intend to ship for.

jldupont
You mean XYZ_app as in: "erl -boot start_sasl -s XYZ_app start", and 'start' is the default if omitted.
Christian
@Christian: good catch.. I'll update.
jldupont
Boot files don't have to be constraining. I use a make file with a make boot target that rebuilds the boot file for the currently available erlang distribution. And boot files do make starting a service dead easy.
Jeremy Wall
@Jeremy Wall: your method sort of defeats the purpose then.
jldupont
not really. Boot files have just one purpose. Describe a startup environment for your app. *.rel files which are used to create the boot file have multiple purposes one of which is to describe a release for your app. I may be a little liberal with that purpose but the ease of startup with the boot file is more than useful enough to warrant it.
Jeremy Wall
A: 

You can use erl -make directly, as it assumes all *.erl files should be compiled to *.beam, and skip it if they haven't been changed since last compile. Check out the make module for what you can put in your Emakefile to change certain behavior.

Using plain Makefile to invoke this compile strategy also gives other users a familiar build-procedure.

Having a bootstrap start function as jldupont suggests is a nice way to start up your development environment. It is even sufficiently fine for using in production to some point.

The next step is to use full OTP (*.rel) release files and generate boot scripts that starts all the applications in the order the *.app specify dependencies on each other.

Christian
A: 

Two ways

precompile your code

$ erlc module1.erl
$ erl -s module1 funcname arg1 arg2 ....

Or use escript

#!/usr/bin/env escript
main() ->
    module1:fun(...)

escript is a scripting interface to Erlang

ja
A: 

Check out Sinan and Faxien: http://www.erlware.org

Tristan Sloughter