[erlang-questions] starting and cleanly stopping erlang

Serge Aleynikov <>
Tue Jan 16 14:13:28 CET 2007

Joe Armstrong wrote:
> I have written a linked-in driver - that appears to work. Let's assume it
> is started with myLinkedInDriver:start() and stopped with
> myLinkedInDriver:stop()
> I want to do the following.
>    - automatically start the linked-in driver when I start Erlang
>    - automatically stop the linked in driver when I stop Erlang
> I want to stop the linked in driver in a controlled manner. ie I want
> *something* to call
> myLinkedInDriver:stop() and allow me to close the thing down in an orderly
> manner.
> If my linked in driver is not correctly closed down then I'll have to do a
> whole lot of work
> the next time start the system.
> What the minimal amount of junk I have to use to get this behaviour?
> I think I need a minimum of an OTP application + a boot script + some
> command
> line arguments to erl to disable control C handling etc.

1. An OTP application with a gen_server process for your driver

-export([ ..., init/1 | OtherGenServerCallbacks ]).

init(Args) ->
     process_flag(trap_exit, true),  % terminate/2 won't be called unless
                                     % you trap exit signals
     case erl_ddll:load_driver(Path, Name) of
     ok ->
         Port = open_port({spawn, ?MODULE}, []),
         % Do other initialization here
         {ok, State};
     {error, Reason} ->
         {stop, Reason}

terminate(shutdown, State) ->
     % do required clean up here

2. An application module and supervisor module that starts your 
gen_server. (Callbacks for the two can be implemented in the same module).

3. An *.app file listing your application modules
4. A *.rel file needed to generate a boot script
    (use "erlc YourApp.rel" to generate a boot script)
5. If you don't plan to do an embedded system and not interested in 
logging, and release handling, then merely getting an emulator to 
start/stop your app in a controlled manner can be accomplished by:

erl -noshell -detach -pa $YOUR_APP/ebin +Bi -boot $APP_NAME

6. If auto-restart of erl is needed, set the HEART_COMMAND env option, 
and use "-heart" command-line option at startup above.

7. An alternative (advised) way to start a node is by using run_erl 
(that allows to do logging of console output, gives login to the console 
using to_erl, and does log rotation).  See:

and also be careful about using -heart option of erl together with 
run_erl.  Details and patch are here:


8. SysV startup/shutdown script that allows to start/stop the node in a 
controlled manner (i.e. doing shutdown via starting a remote node and 
issuing init:stop() using rpc or else using erl_call utility with an 
'-t' option that requires this patch:



> Does anybody know how do do this?
> /Joe
> ------------------------------------------------------------------------
> _______________________________________________
> erlang-questions mailing list
> http://www.erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list