init(3erl) | Erlang Module Definition | init(3erl) |
init - Coordination of system startup.
This module is preloaded and contains the code for the init system process that coordinates the startup of the system. The first function evaluated at startup is boot(BootArgs), where BootArgs is a list of command-line arguments supplied to the Erlang runtime system from the local operating system; see erl(1).
init reads the boot script, which contains instructions on how to initiate the system. For more information about boot scripts, see script(5).
init also contains functions to restart, reboot, and stop the system.
boot(BootArgs) -> no_return()
Types:
Starts the Erlang runtime system. This function is called when the emulator is started and coordinates system startup.
BootArgs are all command-line arguments except the emulator flags, that is, flags and plain arguments; see erl(1).
init interprets some of the flags, see section Command-Line Flags below. The remaining flags ("user flags") and plain arguments are passed to the init loop and can be retrieved by calling get_arguments/0 and get_plain_arguments/0, respectively.
get_argument(Flag) -> {ok, Arg} | error
Types:
Returns all values associated with the command-line user flag Flag. If Flag is provided several times, each Values is returned in preserved order. Example:
% erl -a b c -a d ... 1> init:get_argument(a). {ok,[["b","c"],["d"]]}
The following flags are defined automatically and can be retrieved using this function:
2> init:get_argument(root). {ok,[["/usr/local/otp/releases/otp_beam_solaris8_r10b_patched"]]}
3> init:get_argument(progname). {ok,[["erl"]]}
4> init:get_argument(home). {ok,[["/home/harry"]]}
Returns error if no value is associated with Flag.
get_arguments() -> Flags
Types:
Returns all command-line flags and the system-defined flags, see get_argument/1.
get_plain_arguments() -> [Arg]
Types:
Returns any plain command-line arguments as a list of strings (possibly empty).
get_status() -> {InternalStatus, ProvidedStatus}
Types:
internal_status() = starting | started | stopping
The current status of the init process can be inspected. During system startup (initialization), InternalStatus is starting, and ProvidedStatus indicates how far the boot script has been interpreted. Each {progress, Info} term interpreted in the boot script affects ProvidedStatus, that is, ProvidedStatus gets the value of Info.
reboot() -> ok
All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system terminates. If command-line flag -heart was specified, the heart program tries to reboot the system. For more information, see heart(3erl).
To limit the shutdown time, the time init is allowed to spend taking down applications, command-line flag -shutdown_time is to be used.
restart() -> ok
The system is restarted inside the running Erlang node, which means that the emulator is not restarted. All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system is booted again in the same way as initially started. The same BootArgs are used again.
To limit the shutdown time, the time init is allowed to spend taking down applications, command-line flag -shutdown_time is to be used.
script_id() -> Id
Types:
Gets the identity of the boot script used to boot the system. Id can be any Erlang term. In the delivered boot scripts, Id is {Name, Vsn}. Name and Vsn are strings.
stop() -> ok
The same as stop(0).
stop(Status) -> ok
Types:
All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system terminates by calling halt(Status). If command-line flag -heart was specified, the heart program is terminated before the Erlang node terminates. For more information, see heart(3erl).
To limit the shutdown time, the time init is allowed to spend taking down applications, command-line flag -shutdown_time is to be used.
The support for loading of code from archive files is experimental. The only purpose of releasing it before it is ready is to obtain early feedback. The file format, semantics, interfaces, and so on, can be changed in a future release. The -code_path_choice flag is also experimental.
The init module interprets the following command-line flags:
This flag is particular useful when you want to elaborate with code loading from archives without editing the boot script. For more information about interpretation of boot scripts, see script(5). The flag has also a similar effect on how the code server works; see code(3erl).
% erl -noshell -eval 'R = 16#1F+16#A0, io:format("~.16B~n", [R])' \\ -s erlang halt BF
If multiple -eval expressions are specified, they are evaluated sequentially in the order specified. -eval expressions are evaluated sequentially with -s and -run function calls (this also in the order specified). As with -s and -run, an evaluation that does not terminate blocks the system initialization process.
Example:
% erl -run foo -run foo bar -run foo bar baz 1 2
This starts the Erlang runtime system and evaluates the following functions:
foo:start() foo:bar() foo:bar(["baz", "1", "2"]).
The functions are executed sequentially in an initialization process, which then terminates normally and passes control to the user. This means that a -run call that does not return blocks further processing; to avoid this, use some variant of spawn in such cases.
Example:
% erl -s foo -s foo bar -s foo bar baz 1 2
This starts the Erlang runtime system and evaluates the following functions:
foo:start() foo:bar() foo:bar([baz, '1', '2']).
The functions are executed sequentially in an initialization process, which then terminates normally and passes control to the user. This means that a -s call that does not return blocks further processing; to avoid this, use some variant of spawn in such cases.
Because of the limited length of atoms, it is recommended to use -run instead.
% erl -- a b -children thomas claire -ages 7 3 -- x y ... 1> init:get_plain_arguments(). ["a","b","x","y"] 2> init:get_argument(children). {ok,[["thomas","claire"]]} 3> init:get_argument(ages). {ok, [["7","3"]]} 4> init:get_argument(silly). error
erts 10.2.4 | Ericsson AB |