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

Kevin <>
Tue Feb 17 14:31:55 CET 2009


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.

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>


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

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}.







More information about the erlang-questions mailing list