[erlang-questions] managing OS processes

Ciprian Dorin Craciun ciprian.craciun@REDACTED
Tue May 29 11:21:51 CEST 2012


On Sat, May 26, 2012 at 3:12 PM, Gleb Peregud <gleber.p@REDACTED> wrote:
> On Sat, May 26, 2012 at 11:06 AM, Benoit Chesneau <bchesneau@REDACTED> wrote:
>> I'm trying to find a way to manage some os processes launched from
>> Erlang. For example I need to relaunch them when the external process
>> die for an unknown reason or send an HUP signal or just stop the
>> external process.
>
> Hello Benoit
>
> Looks like there are two of us, who needs a better mechanism to manage
> OS processes from Erlang. I need it for a CI server which I'm working
> on, so my requirements are not as high as yours, but still os:cmd
> doesn't work for me and ports are missing one feature which I need.
>
> Maybe we could reanimate erlexec together?
>
> Cheers,
> Gleb


    Hello all!

    I don't think its the right way (although it seems to be suggested
by the Erlang manual or somewhere else I've read it), but the way I
did it was to write a small C program that is the one "babysitting"
the real process, and a small Erlang wrapper.

    The code is here (Apache 2.0 licensed):
      https://github.com/cipriancraciun/mosaic-node/tree/development/applications/mosaic-harness/sources
      https://github.com/cipriancraciun/mosaic-node/blob/development/applications/mosaic-harness/sources/mosaic_harness_backend.erl
      https://github.com/cipriancraciun/mosaic-node/blob/development/applications/mosaic-harness/sources/mosaic_harness.c

    The C program allows the following:
    * it uses a small protocol based on JSON to communicate with the
Erlang wrapper, using solely `stdin` / `stdout` between itself and
Erlang, and between itself and the "babysitted" program;
    * it allows a single process to be executed at a time (controlling
environment, arguments, and working directory); (it could be extended
to multiple process in parallel if wanted;) (but you can run it again
if you want;)
    * it allows a signal to be sent;
    * it allows "exchange" messages to be sent to and from Erlang and
the process;
    * it allows brutal termination; (i.e. SIGKILL;)
    * if `stdin` is closed by the Erlang side it kills the process;
    * if `stdin` is closed by the process side it informs the Erlang side;

    The C part is a self-contained `.c` file (of about 2k lines, of
which 25% are structure definitions), written in an event driven
manner (no threads), statically linkable, depending only on the
`jansson` library (for JSON), using mostly POSIX syscalls.

    I've successfully used it for about a year now (in non-production
systems) and it behaves nicely.

    If someone finds it useful I could try to extract it in a
standalone project.

    Ciprian.



More information about the erlang-questions mailing list