0

I wrote a supervisor (shown below).

It only has one child process that I get from using locations:start_link/0. I expect it to start up a supervisor and register itself globally. That way, I can get to by using global:whereis_name/1.

When I start the supervisor through the shell it works as expected:

 $ erl
 1> locator_suo:start_link().
 registering global supervisor
 starting it....
 supervisor <0.34.0>
 {ok,<0.34.0>}

Then I can get to it by its global name, locator_sup:

2> global:whereis_name( locator_sup ).
<0.34.0>

But I want to start the system using a startup script, so I tried starting the system like so:

$ erl -s locator_sup start_link
registering global supervisor
starting it....
supervisor <0.32.0>

It seems that the init function for the supervisor is being called, but when I try to find the supervisor by its global name, I get undefined

1> global:whereis_name( locator_sup ).
undefined

So my question is, why does the supervisor process only get registered when I use start_link from the shell?

The supervisor module:

-module(locator_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
    io:format( "registering global supervisor\n" ), 
    {ok, E} = supervisor:start_link({global, ?MODULE}, ?MODULE, []),
    io:format("supervisor ~p\n", [E] ),
    {ok,  E}.

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

% only going to start the gen_server that keeps track of locations
init(_) ->
   io:format( "starting it....\n" ),
   {ok, {{one_for_one, 1, 60},
    [{locations, {locations, start_link, []},
     permanent, brutal_kill, worker, [locations]}]}}.
Lee Avital
  • 542
  • 6
  • 15
  • When you use the startup script, how do you connect to the shell (to run `global:whereis_name/1`)? – johlo Oct 18 '13 at 22:20
  • The startup script leaves you in a shell. It's not much a script, just the command ```erl -s locator_sup start_link``` – Lee Avital Oct 18 '13 at 23:57
  • 3
    it looks like your supervisor dies immediately after being started. try `is_process_alive(list_to_pid("<0.32.0>"))`. This post might help: http://stackoverflow.com/questions/6174079/problem-with-starting-supervisor-through-script-erlang. So essentially your question is a duplicate. – akonsu Oct 20 '13 at 03:24

1 Answers1

0

One reason you may have that it is because you start your node not in distributed mode.

First of all add such params to see what happens during startup: erl -boot start_sasl. Second add node name (it will automatically enable distributed mode) : ... -sname my_node

So the startup command will look like:

erl -boot start_sasl -sname my_node -s locator_sup start_link
danechkin
  • 1,306
  • 8
  • 15