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]}]}}.
3
  • When you use the startup script, how do you connect to the shell (to run global:whereis_name/1)? Commented Oct 18, 2013 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 Commented Oct 18, 2013 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: stackoverflow.com/questions/6174079/…. So essentially your question is a duplicate. Commented Oct 20, 2013 at 3:24

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.