Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

erlang - startup script

Tags:

erlang

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 ?

like image 297
ceth Avatar asked Dec 08 '09 10:12

ceth


People also ask

How do I start Erlang?

On a Unix system you enter the command erl at the operating system prompt. This command starts the Erlang runtime system and the Erlang shell. On the Windows platform you normally start Erlang/OTP from the start menu. You can also enter the command erl or werl from a DOS box or Command box.

What is an Escript file?

escript provides support for running short Erlang programs without having to compile them first and an easy way to retrieve the command line arguments.

How do I stop Erlang process?

A process can terminate itself by calling one of the BIFs exit(Reason), erlang:error(Reason), erlang:error(Reason, Args), erlang:fault(Reason) or erlang:fault(Reason, Args). The process then terminates with reason Reason for exit/1 or {Reason,Stack} for the others.


2 Answers

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.

like image 143
jldupont Avatar answered Nov 16 '22 01:11

jldupont


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
like image 44
Emil Ivanov Avatar answered Nov 16 '22 01:11

Emil Ivanov