System
The System module provides access to variables used or maintained by the VM and to functions that interact directly with the VM or the host system.
Summary
| argv() | List command line arguments |
| argv(args) | Modify command line arguments |
| at_exit(fun) | Register a program exit handler function |
| build_info() | Elixir build information |
| cmd(command) | Execute a system command |
| cwd!() | Current working directory, exception on error |
| cwd() | Current working directory |
| find_executable(program) | Locate an executable on the system |
| get_env() | System environment variables |
| get_env(varname) | Environment variable value |
| get_pid() | Erlang VM process identifier |
| halt(status \\ 0) | Halt the Erlang runtime system |
| put_env(dict) | Set multiple environment variables |
| put_env(varname, value) | Set an environment variable value |
| stacktrace() | Last exception stacktrace |
| tmp_dir!() | Writable temporary directory, exception on error |
| tmp_dir() | Writable temporary directory |
| user_home!() | User home directory, exception on error |
| user_home() | User home directory |
| version() | Elixir version information |
Functions
Specs:
- argv :: [String.t]
List command line arguments.
Returns the list of command line arguments passed to the program.
Specs:
- argv([String.t]) :: :ok
Modify command line arguments.
Changes the list of command line arguments. Use it with caution, as it destroys any previous argv information.
Register a program exit handler function.
Registers a function that will be invoked at the end of program execution. Useful for invoking a hook in "script" mode.
The function must receive the exit status code as an argument.
Specs:
- build_info :: Keyword.t
Elixir build information.
Returns a keyword list with Elixir version, git tag info and compilation date.
Specs:
- cmd(char_list) :: char_list
- cmd(binary) :: binary
Execute a system command.
Executes command in a command shell of the target OS,
captures the standard output of the command and returns
the result as a binary.
If command is a char list, a char list is returned.
Returns a binary otherwise.
Current working directory.
Returns the current working directory or nil if one
is not available.
Current working directory, exception on error.
Returns the current working directory or raises RuntimeError.
Specs:
- find_executable(char_list) :: char_list | nil
- find_executable(binary) :: binary | nil
Locate an executable on the system.
This function looks up an executable program given
its name using the environment variable PATH on Unix
and Windows. It also considers the proper executable
extension for each OS, so for Windows it will try to
lookup files with .com, .cmd or similar extensions.
If program is a char list, a char list is returned.
Returns a binary otherwise.
Specs:
System environment variables.
Returns a list of all environment variables. Each variable is given as a
{name, value} tuple where both name and value are strings.
Specs:
- get_env(binary) :: binary | nil
Environment variable value.
Returns the value of the environment variable
varname as a binary, or nil if the environment
variable is undefined.
Specs:
- get_pid :: binary
Erlang VM process identifier.
Returns the process identifier of the current Erlang emulator in the format most commonly used by the operating system environment.
See http://www.erlang.org/doc/man/os.html#getpid-0 for more info.
Specs:
- halt(non_neg_integer | binary | :abort) :: no_return
Halt the Erlang runtime system.
Halts the Erlang runtime system where the argument status must be a
non-negative integer, the atom :abort or a binary.
If an integer, the runtime system exits with the integer value which is returned to the operating system;
If
:abort, the runtime system aborts producing a core dump, if that is enabled in the operating system;If a char list, an erlang crash dump is produced with status as slogan, and then the runtime system exits with status code 1;
Note that on many platforms, only the status codes 0-255 are supported by the operating system.
For more information, check: http://www.erlang.org/doc/man/erlang.html#halt-1
Examples
System.halt(0)
System.halt(1)
System.halt(:abort)
Specs:
- put_env(Dict.t) :: :ok
Set multiple environment variables.
Sets a new value for each environment variable corresponding
to each key in dict.
Specs:
- put_env(binary, binary) :: :ok
Set an environment variable value.
Sets a new value for the environment variable varname.
Last exception stacktrace.
Note that the Erlang VM (and therefore this function) does not return the current stacktrace but rather the stacktrace of the latest exception.
Writable temporary directory.
Returns a writable temporary directory. Searches for directories in the following order:
- The directory named by the TMPDIR environment variable
- The directory named by the TEMP environment variable
- The directory named by the TMP environment variable
C:\TMPon Windows or/tmpon Unix- As a last resort, the current working directory
Returns nil if none of the above are writable.
Writable temporary directory, exception on error.
Same as tmp_dir/0 but raises RuntimeError
instead of returning nil if no temp dir is set.
User home directory.
Returns the user home directory (platform independent).
Returns nil if no user home is set.
User home directory, exception on error.
Same as user_home/0 but raises RuntimeError
instead of returning nil if no user home is set.