Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running erlang shell as a daemon/service

I have an Erlang program that runs in Erlang shell, obviously, and I want to monitor it.

This is what I want:

  • When the machine starts the Erlang shell should start up with it, and the program that runs in the shell too.
  • If the Erlang shell crashes for some reason it should get restarted.
  • You should be able to manually start/stop/restart the Erlang shell.

Example:

/etc/init.d/foobar start
/etc/init.d/foobar stop
/etc/init.d/foobar restart

I haven't started with the whole "restart itself if crash" thing yet, got stuck with the easy thing, or is it easy?

What I have done is this:

Taken the skeleton code from /etc/init.d/skeleton and replaced the PATH, DESC, NAME etc etc... This works, I can do:

/etc/init.d/foobar start

However, I cant stop it... The thing is that I start the Erlang shell with "erl" which is a script that does some fancy things that I dont understand. One thing it does is, it creates a very long and complex process name. It's not just "erl" it's like:

/usr/lib/erlang/erts-5.7.4/bin/beam.smp -- -root /usr/lib/erlang -progname erl -- -home /home/xxx -- .... and some more.

Is there a better way to do this?

OS: Ubuntu 11.04

like image 494
red-nuht Avatar asked Jul 26 '11 13:07

red-nuht


2 Answers

In addition to creating a target release, a standard Erlang production environment as recommended by @Martin, you need the following:

  • To allow for automatic restart of a crashed node, you should use the heart functionality.

  • To stop a running Erlang node, you could start up a temporary Erlang node, connect to the running node and issue a stop command:

    erl -noshell -sname temp_control \
        -eval "rpc:call(mynode@myhost, init, stop, [])" \
        -s init stop
    
    • noshell disables input and shell output
    • sname sets the name for the temporary node
    • eval let's you execute any valid Erlang expression
      • rpc:call(Node, M, F, A) will call M:F(A) on the node specified (A is list of arguments that will be passed to the function as real arguments)
    • s M F runs the function M:F()

    (eval and s are run in sequence)

like image 187
Adam Lindberg Avatar answered Nov 15 '22 04:11

Adam Lindberg


What you want to do is create a target-system. The documentation for doing so is here: http://www.erlang.org/doc/system_principles/create_target.html However, it is a bit complicated at first, until you get the basic concepts.

Roughly speaking, you do the following:

  1. Create an empty node. That is, the 'bin, erts and releases' directories (with updated scripts in bin).
  2. Create a release via release_tools as described in the dox.
  3. Unpack the release on the empty node, set the release/start_erl.data to point to the new release and erts versions.

This can then be managed as a service with restarts/monitors and whatever you like to add.

like image 35
Martin Carlson Avatar answered Nov 15 '22 03:11

Martin Carlson