[erlang-questions] What's the proper/right way to run erlang in the background?

Robert Virding <>
Mon Oct 28 16:56:02 CET 2013


Yes, when you start erl with -s or -run options then a separate process is started by init to evaluate these calls. This process then terminates after the calls have completed. In your first version this caused the supervisors to terminate as they were (correctly) started with a start_link. In your second version the added receive meant that that call never returned and hence the start process never terminated. 

There is a better explanation in http://www.erlang.org/doc/man/init.html . 

Robert 

----- Original Message -----

> From: "fxmy wang" <>
> To: "Michael Scofield" <>
> Cc: 
> Sent: Saturday, 26 October, 2013 4:02:14 PM
> Subject: Re: [erlang-questions] What's the proper/right way to run erlang in
> the background?

> Could it because that you are using my_supervisor:start_link(), so the
> start() function and the top supervisor is linked .
> Then once the start() function executes and quits, your top supervisor also
> quits.

> Then brings your whole application down .

> After you added an infinite receive, you block your start() function from
> exciting , thus your whole application is ruining fine ?
> 2013年10月21日 下午9:33于 "Michael Scofield" <  >写道:

> > Hi all! I'm new to erlang for a few month.
> 

> > Recently I've developed a small pure erlang program, and it runs well in
> > the
> > shell. Now I want to run it in the background, as a daemon service process
> > in the os (mine is Ubuntu 12.04 64bit with R16B). So I googled "run erlang
> > without shell", and google gave me some advice:
> 

> > 1. Using escript. -This is what erlang official faq suggested (
> > http://www.erlang.org/faq/how_do_i.html ).
> 
> > 2. erl -detached -s Module Function Args
> 

> > I tried both. As to #1, my escript goes to
> 

> > #!/usr/bin/env escript
> 
> > main(_) ->
> 
> > my_supervisor:start_link(arg1, arg2).
> 

> > I ran it using "nohup ./my-escript &", and it didn't run my code. =(
> 

> > So I went to #2. I was acknowledged that "-s" can only pass 0 or 1
> > argument,
> > so I wrapped my supervisor using
> 

> > -module(run).
> 
> > -export([start/0]).
> 

> > start() ->
> 
> > my_supervisor:start_link(arg1, arg2).
> 

> > and ran my code as "erl -detached -s run start". But this was also a dead
> > end.
> 

> > I guessed it's because the run:start/0 just returned and finished the
> > shell(Is it?), so I added a line to the run:start/0:
> 

> > -module(run).
> 
> > -export([start/0]).
> 

> > start() ->
> 
> > my_supervisor:start_link(arg1, arg2),
> 
> > receive after infinity -> stop end.
> 

> > And executed "erl -detached -s run start", and everything goes fine!
> > my_supervisor started to work without the shell!
> 

> > Though the problem "solved", I'm very very confused.
> 
> > 1. Why the "magic line" solved my problem?
> 
> > 2. What exactly is the proper or right way to run an erlang application in
> > the background as the command "nohup ... &" does?
> 

> > Thanks in advance!
> 

> > Michael
> 

> > _______________________________________________
> 
> > erlang-questions mailing list
> 
> > 
> 
> > http://erlang.org/mailman/listinfo/erlang-questions
> 

> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131028/957fb901/attachment.html>


More information about the erlang-questions mailing list