[erlang-questions] supervisor behaves differently if started from command line

Essien Essien <>
Tue Feb 17 16:30:48 CET 2009


Hi Kevin,

On Tue, Feb 17, 2009 at 2:31 PM, Kevin <> wrote:
>
> Hello,  I'll post the relevant two files below.  One is a supervisor and
> the other a gen_server, and both are barely anything more than slightly
> modified skeletons from the erlang emacs mode.
>
> When I run the supervisor from the command line "erl -s mysup" I find
> that all the startup and init functions are called, but
> then there is nothing there, whereis(mygen) returns undefined and a cal
> to mygen:doit() returns a noproc error.

The supervisor has run and exited, taking with it all its child processes.

>
> However, when I run this from the erlang shell I have no problems
>
> Eshell V5.6.5  (abort with ^G)
> 1> mysup:start().
> {ok,<0.32.0>}
> 2> whereis(mygen).
> <0.33.0>
> 3> mygen:doit("asdf").
> in handle call, request is "asdf"
> ok
> 4>

The supervisor is still alive.
>
>
> When I put in io:format debug statements in, I find that all the same
> functions are called, its just that starting from the command line seems
> to be shooting blanks.
>
> I hope this is not because I decided to decapitate my code of the
> "application" behavior.  I did that because its just too many layers, I
> like to keep
> things simple.  Is it ok to use a supervisor at the top? I think it
> makes more sense if you are starting things with a unix style init script

Actually that is the problem. Other more experienced users may have
more to say here,
but from what I've observed, a supervisor is _suposed_ to be started
by an application, not alone.

If you check the man page for supervisor, you'll notice there's no
start function, only start_link. And start_link, creates a link to the
calling process, which means that once the calling process runs to an
end and dies, the called process will also die. When you invoke your
supervisor as

erl -s mysup

The top level starts mysup.erl, runs the init callback and exits hence
taking the supervisor with it. But when you start it via an
application, the application blocks till it is explicitly killed.

Hope that helps. (I'm also somewhat of an erlang newbie :) )

cheers,
Essien
>
> Thanks!
>
>
> === mysup.erl =========================================
>
> -module(mysup).
> -behaviour(supervisor).
> -export([start/0, start_link/0]).
>
> -export([init/1]).
>
> -define(SERVER, ?MODULE).
>
> start() ->
>    start_link().
>
> start_link() ->
>    supervisor:start_link({local, ?SERVER}, ?MODULE, []).
>
> init([]) ->
>    AChild = {mygen,{mygen,start_link,[]},
>              permanent,2000,worker,[]},
>    {ok,{{one_for_one,10,10}, [AChild]}}.
>
>
> === mygen.erl ===================================
> -module(mygen).
> -behaviour(gen_server).
> -export([start_link/0, doit/1]).
> -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
>         terminate/2, code_change/3]).
>
> -record(state, {}).
>
> start_link() ->
>    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
>
> init([]) ->
>    {ok, #state{}}.
>
> doit(Text) ->
>    gen_server:call(?MODULE, Text).
>
> handle_call(Request, _From, State) ->
>    io:format("in handle call, request is ~p~n", [Request]),
>    {reply, ok, State}.
>
> handle_cast(_Msg, State) ->
>    {noreply, State}.
>
> handle_info(_Info, State) ->
>    {noreply, State}.
>
> terminate(_Reason, _State) ->
>    ok.
>
> code_change(_OldVsn, State, _Extra) ->
>    {ok, State}.
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list