init (erts v16.1.1-rc0)
View SourceCoordination 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(4).
init also contains functions to restart, reboot, and stop the system.
Command-Line Flags
Warning
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 init module interprets the following command-line flags:
--- Everything following--up to the next flag is considered plain arguments and can be retrieved usingget_plain_arguments/0.-code_path_choice Choice- Can be set tostrictorrelaxed. It controls how each directory in the code path is to be interpreted:- Strictly as it appears in the
boot script, or initis to be more relaxed and try to find a suitable directory if it can choose from a regularebindirectory and anebindirectory in an archive file.
It defaults to
strictfrom OTP 27 and this option is scheduled for removal in OTP 28.- Strictly as it appears in the
-epmd_module Module- This flag is deprecated and has been replaced by thekernelapplication parameterepmd_module.-eval Expr- Scans, parses, and evaluates an arbitrary expressionExprduring system initialization. If any of these steps fail (syntax error, parse error, or exception during evaluation), Erlang stops with an error message. In the following example Erlang is used as a hexadecimal calculator:% erl -noshell -eval 'R = 16#1F+16#A0, io:format("~.16B~n", [R])' \\ -s erlang halt BFIf multiple
-evalexpressions are specified, they are evaluated sequentially in the order specified.-evalexpressions are evaluated sequentially with-sand-runfunction calls (this also in the order specified). As with-sand-run, an evaluation that does not terminate blocks the system initialization process.-extra- Everything following-extrais considered plain arguments and can be retrieved usingget_plain_arguments/0.Example:
% erl -extra +A 1 -- ... 1> init:get_plain_arguments(). ["+A","1","--"]The
-extraflag can be passed on the command line, throughERL_*FLAGSor-args_file. It only effects the remaining command-line flags in the entity in which it is passed. If multiple-extraflags are passed they are concatenated using the same order rules asERL_*FLAGSor-args_filein which they are given.Example:
% ERL_AFLAGS="-extra a" ERL_ZFLAGS="-extra d" erl -extra b -extra c ... 1> init:get_plain_arguments(). ["a","b","-extra","c","d"]-S Mod [Func [Arg1, Arg2, ...]]- Evaluates the specified function call during system initialization.Funcdefaults tostart. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list[Arg1,Arg2,...]as argument. All arguments are passed as strings. If an exception is raised, Erlang stops with an error message.Example:
% erl -S httpd serve --port 8080 /var/www/htmlThis starts the Erlang runtime system and evaluates the function
httpd:serve(["--port", "8080", "/var/www/html"]). All arguments up to the end of the command line will be passed to the called function.The function is executed sequentially in an initialization process, which then terminates normally and passes control to the user. This means that a
-Scall that does not return blocks further processing; to avoid this, use some variant ofspawnin such cases.The
-Sflag is only allowed on the command line. If passed throughERL_*FLAGSor-args_fileit will be parsed as a normal command line flag.-run Mod [Func [Arg1, Arg2, ...]]- Evaluates the specified function call during system initialization.Funcdefaults tostart. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list[Arg1,Arg2,...]as argument. All arguments are passed as strings. If an exception is raised, Erlang stops with an error message.Example:
% erl -run foo -run foo bar -run foo bar baz 1 2This 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
-runcall that does not return blocks further processing; to avoid this, use some variant ofspawnin such cases.Note
This flag will not forward arguments beginning with a hyphen (-) to the specified function, as these will be interpreted as flags to the runtime. If the function uses flags in this form, it is advised to use
-Sinstead.-s Mod [Func [Arg1, Arg2, ...]]- Evaluates the specified function call during system initialization.Funcdefaults tostart. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list[Arg1,Arg2,...]as argument. All arguments are passed as atoms. If an exception is raised, Erlang stops with an error message.Example:
% erl -s foo -s foo bar -s foo bar baz 1 2This 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
-scall that does not return blocks further processing; to avoid this, use some variant ofspawnin such cases.Because of the limited length of atoms, it is recommended to use
-runinstead.Note
This flag will not forward arguments beginning with a hyphen (-) to the specified function, as these will be interpreted as flags to the runtime. If the function uses flags in this form, it is advised to use
-Sinstead, with the additional caveat that arguments are passed as strings instead of atoms.
Example
% 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).
errorSee Also
Summary
Functions
Starts the Erlang runtime system.
Returns all values associated with the command-line user flag Flag.
Returns all command-line flags and the system-defined flags, see
get_argument/1.
Returns any plain command-line arguments as a list of strings (possibly empty).
The current status of the init process can be inspected.
Reboot the Erlang node.
The same as restart([]).
Restart all Erlang applications.
Gets the identity of the boot script used to boot the system.
Stop the Erlang node.
Types
Functions
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.
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:
root- The installation directory of Erlang/OTP,$ROOT:2> init:get_argument(root). {ok,[["/usr/local/otp/releases/otp_beam_solaris8_r10b_patched"]]}progname- The name of the program which started Erlang:3> init:get_argument(progname). {ok,[["erl"]]}home- The home directory (on Unix, the value of $HOME):4> init:get_argument(home). {ok,[["/home/harry"]]}
Returns error if no value is associated with Flag.
Returns all command-line flags and the system-defined flags, see
get_argument/1.
-spec get_plain_arguments() -> [Arg] when Arg :: string().
Returns any plain command-line arguments as a list of strings (possibly empty).
-spec get_status() -> {InternalStatus, ProvidedStatus} when InternalStatus :: internal_status(), ProvidedStatus :: term().
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.
-spec reboot() -> ok.
Reboot the Erlang node.
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.
To limit the shutdown time, the time init is allowed to spend taking down
applications, command-line flag -shutdown_time is to be used.
-spec restart() -> ok.
The same as restart([]).
-spec restart([{mode, mode()}]) -> ok.
Restart all Erlang applications.
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 when restarting the system unless the mode option
is given, allowing the code loading mode to be set to either embedded or
interactive. All other BootArgs remain the same.
To limit the shutdown time, the time init is allowed to spend taking down
applications, command-line flag -shutdown_time is to be used.
-spec script_id() -> Id when Id :: term().
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.
-spec stop() -> ok.
The same as stop(0).
-spec stop(Status) -> ok when Status :: non_neg_integer() | string().
Stop the Erlang node.
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.
To limit the shutdown time, the time init is allowed to spend taking down
applications, command-line flag -shutdown_time is to be used.