Home > Erlang > You should not start your supervisors like this!

You should not start your supervisors like this!

Twice a week I go to StackOverflow and read the new posts about Erlang, doing this I have seen many questions about starting a supervisor using the erl command and the “- s” flag.

This is a topic I really care about, actually when I was coding my first stuff in Erlang, I was asked to code and start from a unix script a small
system composed of a gen_server which was spawned and controlled by a supervisor.

At that time I did’t know Erlang application so I decided to do something like this:

erl -pa ebin -sname mynode -cookie mycookie -s mysupervisor start_link

but this leaded nowhere, the supervisor were not started at all and this was
really frustrating. The thing that annoyed me the most was that by using:

erl -pa ebin -sname mynode -cookie mycookie -s myserver start_link

the gen_server was started successfully, so I thought that the problem was in the supervisor but in fact it wasn’t.

Where was the problem here? Probably, the thing most of people don’t know (and for sure the one I did’t know at the time) is that the supervisor process is linked to the shell process in which it is started. When the calling process (the shell) exits, the supervisor is taken down as well.

To tackle this problem you could take two different approaches:

1) unlink the supervisor from its calling process by using unlink/0
2) package your code using an application

The first solution can be achieved by changing the start_link/0 function of the supervisor as follows:

start_link() -> 
 {ok, Pid} = supervisor:start_link({local, ?SERVER}, ?MODULE, []), 

Altough this solution solves your problems, I strongly suggest you to adopt the second solution I listed above. Applications in Erlang are really easy to understand and implement. Many information can be gathered online, for example in Mitchell Hashimoto’s blog or in the standard documentation.

Categories: Erlang Tags: , , ,
  1. January 3, 2012 at 7:17 am

    How about just calling supervisor:start instead of start_link?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: