System — Elixir v1.18.3 (original) (raw)

The System module provides functions that interact directly with the VM or the host system.

Time

The System module also provides functions that work with time, returning different times kept by the system with support for different time units.

One of the complexities in relying on system times is that they may be adjusted. For example, when you enter and leave daylight saving time, the system clock will be adjusted, often adding or removing one hour. We call such changes "time warps". In order to understand how such changes may be harmful, imagine the following code:

## DO NOT DO THIS
prev = System.os_time()
# ... execute some code ...
next = System.os_time()
diff = next - prev

If, while the code is executing, the system clock changes, some code that executed in 1 second may be reported as taking over 1 hour! To address such concerns, the VM provides a monotonic time via System.monotonic_time/0 which never decreases and does not leap:

## DO THIS
prev = System.monotonic_time()
# ... execute some code ...
next = System.monotonic_time()
diff = next - prev

Generally speaking, the VM provides three time measurements:

The time functions in this module work in the :native unit (unless specified otherwise), which is operating system dependent. Most of the time, all calculations are done in the :native unit, to avoid loss of precision, with convert_time_unit/3 being invoked at the end to convert to a specific time unit like:millisecond or :microsecond. See the time_unit/0 type for more information.

For a more complete rundown on the VM support for different times, see the chapter on time and time correctionin the Erlang docs.

Summary

Types

The time unit to be passed to functions like monotonic_time/1 and others.

Functions

Lists command line arguments.

Modifies command line arguments.

Registers a program exit handler function.

Elixir build information.

Executes the given command with args.

Returns the endianness the system was compiled with.

Converts time from time unit from_unit to time unit to_unit.

cwd() deprecated

Current working directory.

cwd!() deprecated

Current working directory, exception on error.

Deletes an environment variable.

Returns the value of the given environment variable or :error if not found.

Returns the value of the given environment variable or raises if not found.

Locates an executable on the system.

Returns all system environment variables.

Returns the value of the given environment variable.

get_pid() deprecated

Erlang VM process identifier.

Immediately halts the Erlang runtime system.

Returns the current monotonic time in the :native time unit.

Returns the current monotonic time in the given time unit.

Checks if the system will halt or not at the end of ARGV processing.

Marks if the system should halt or not at the end of ARGV processing.

Returns the current operating system (OS) time.

Returns the current operating system (OS) time in the given time unit.

Returns the Erlang/OTP release number.

Returns the operating system PID for the current Erlang runtime system instance.

Sets multiple environment variables.

Sets an environment variable value.

Restarts all applications in the Erlang runtime system.

Returns the number of schedulers in the VM.

Returns the number of schedulers online in the VM.

Executes the given command in the OS shell.

stacktrace() deprecated

Deprecated mechanism to retrieve the last exception stacktrace.

Asynchronously and carefully stops the Erlang runtime system.

Returns the current system time in the :native time unit.

Returns the current system time in the given time unit.

Returns the current time offset between the Erlang VM monotonic time and the Erlang VM system time.

Returns the current time offset between the Erlang VM monotonic time and the Erlang VM system time.

Writable temporary directory.

Writable temporary directory, exception on error.

Traps the given signal to execute the fun.

Generates and returns an integer that is unique in the current runtime instance.

Removes a previously registered signal with id.

User home directory, exception on error.

Elixir version information.

Types

@type signal() :: :sigabrt | :sigalrm | :sigchld | :sighup | :sigquit | :sigstop | :sigterm | :sigtstp | :sigusr1 | :sigusr2

@type time_unit() :: :second | :millisecond | :microsecond | :nanosecond | pos_integer()

The time unit to be passed to functions like monotonic_time/1 and others.

The :second, :millisecond, :microsecond and :nanosecond time units controls the return value of the functions that accept a time unit.

A time unit can also be a strictly positive integer. In this case, it represents the "parts per second": the time will be returned in 1 / parts_per_second seconds. For example, using the :millisecond time unit is equivalent to using 1000 as the time unit (as the time will be returned in 1/1000 seconds - milliseconds).

Functions

Lists command line arguments.

Returns the list of command line arguments passed to the program.

Modifies command line arguments.

Changes the list of command line arguments. Use it with caution, as it destroys any previous argv information.

Registers a program exit handler function.

Registers a function that will be invoked at the end of an Elixir script. A script is typically started via the command line via the elixir andmix executables.

The handler always executes in a different process from the one it was registered in. As a consequence, any resources managed by the calling process (ETS tables, open files, and others) won't be available by the time the handler function is invoked.

The function must receive the exit status code as an argument.

If the VM terminates programmatically, via System.stop/1, System.halt/1, or exit signals, the at_exit/1 callbacks are not guaranteed to be executed.

Elixir build information.

Returns a map with the Elixir version, the Erlang/OTP release it was compiled with, a short Git revision hash and the date and time it was built.

Every value in the map is a string, and these are:

One should not rely on the specific formats returned by each of those fields. Instead one should use specialized functions, such as version/0 to retrieve the Elixir version and otp_release/0 to retrieve the Erlang/OTP release.

Examples

iex> System.build_info()
%{
  build: "1.9.0-dev (772a00a0c) (compiled with Erlang/OTP 21)",
  date: "2018-12-24T01:09:21Z",
  otp_release: "21",
  revision: "772a00a0c",
  version: "1.9.0-dev"
}

Executes the given command with args.

command is expected to be an executable available in PATH unless an absolute path is given.

args must be a list of binaries which the executable will receive as its arguments as is. This means that:

This function returns a tuple containing the collected result and the command exit status.

Internally, this function uses a Port for interacting with the outside world. However, if you plan to run a long-running program, ports guarantee stdin/stdout devices will be closed but it does not automatically terminate the program. The documentation for thePort module describes this problem and possible solutions under the "Zombie processes" section.

Windows argument splitting and untrusted arguments

On Unix systems, arguments are passed to a new operating system process as an array of strings but on Windows it is up to the child process to parse them and some Windows programs may apply their own rules, which are inconsistent with the standard C runtime argv parsing

This is particularly troublesome when invoking .bat or .com files as these run implicitly through cmd.exe, whose argument parsing is vulnerable to malicious input and can be used to run arbitrary shell commands.

Therefore, if you are running on Windows and you execute batch files or .com applications, you must not pass untrusted input as arguments to the program. You may avoid accidentally executing them by explicitly passing the extension of the program you want to run, such as .exe, and double check the program is indeed not a batch file or .com application.

Examples

iex> System.cmd("echo", ["hello"])
{"hello\n", 0}

iex> System.cmd("echo", ["hello"], env: [{"MIX_ENV", "test"}])
{"hello\n", 0}

If you want to stream the output to Standard IO as it arrives:

iex> System.cmd("echo", ["hello"], into: IO.stream())
hello
{%IO.Stream{}, 0}

If you want to read lines:

iex> System.cmd("echo", ["hello\nworld"], into: [], lines: 1024)
{["hello", "world"], 0}

Options

Error reasons

If invalid arguments are given, ArgumentError is raised bySystem.cmd/3. System.cmd/3 also expects a strict set of options and will raise if unknown or invalid options are given.

Furthermore, System.cmd/3 may fail with one of the POSIX reasons detailed below:

Shell commands

If you desire to execute a trusted command inside a shell, with pipes, redirecting and so on, please check shell/2.

@spec compiled_endianness() :: :little | :big

Returns the endianness the system was compiled with.

Converts time from time unit from_unit to time unit to_unit.

The result is rounded via the floor function.

convert_time_unit/3 accepts an additional time unit (other than the ones in the time_unit/0 type) called :native. :native is the time unit used by the Erlang runtime system. It's determined when the runtime starts and stays the same until the runtime is stopped, but could differ the next time the runtime is started on the same machine. For this reason, you should use this function to convert :native time units to a predictable unit before you display them to humans.

To determine how many seconds the :native unit represents in your current runtime, you can call this function to convert 1 second to the :nativetime unit: System.convert_time_unit(1, :second, :native).

This function is deprecated. Use File.cwd/0 instead.

Current working directory.

Returns the current working directory or nil if one is not available.

This function is deprecated. Use File.cwd!/0 instead.

Current working directory, exception on error.

Returns the current working directory or raises RuntimeError.

Deletes an environment variable.

Removes the variable varname from the environment.

@spec endianness() :: :little | :big

Returns the endianness.

Returns the value of the given environment variable or :error if not found.

If the environment variable varname is set, then {:ok, value} is returned where value is a string. If varname is not set, :error is returned.

Examples

iex> System.fetch_env("PORT")
{:ok, "4000"}

iex> System.fetch_env("NOT_SET")
:error

Returns the value of the given environment variable or raises if not found.

Same as get_env/1 but raises instead of returning nil when the variable is not set.

Examples

iex> System.fetch_env!("PORT")
"4000"

iex> System.fetch_env!("NOT_SET")
** (System.EnvError) could not fetch environment variable "NOT_SET" because it is not set

Locates an executable on the system.

This function looks up an executable program given its name using the environment variable PATH on Windows and Unix-like operating systems. It also considers the proper executable extension for each operating system, so for Windows it will try to lookup files with .com, .cmd or similar extensions.

Returns all system environment variables.

The returned value is a map containing name-value pairs. Variable names and their values are strings.

Returns the value of the given environment variable.

The returned value of the environment variablevarname is a string. If the environment variable is not set, returns the string specified in default ornil if none is specified.

Examples

iex> System.get_env("PORT")
"4000"

iex> System.get_env("NOT_SET")
nil

iex> System.get_env("NOT_SET", "4001")
"4001"

This function is deprecated. Use System.pid/0 instead.

Erlang VM process identifier.

Returns the process identifier of the current Erlang emulator in the format most commonly used by the operating system environment.

For more information, see :os.getpid/0.

Immediately halts the Erlang runtime system.

Terminates the Erlang runtime system without properly shutting down applications and ports. Please see stop/1 for a careful shutdown of the system.

status must be a non-negative integer, the atom :abort or a binary.

Note that on many platforms, only the status codes 0-255 are supported by the operating system.

For more information, see :erlang.halt/1.

Examples

System.halt(0)
System.halt(1)
System.halt(:abort)

@spec monotonic_time() :: integer()

Returns the current monotonic time in the :native time unit.

This time is monotonically increasing and starts in an unspecified point in time. This is not strictly monotonically increasing. Multiple sequential calls of the function may return the same value.

Inlined by the compiler.

@spec monotonic_time(time_unit() | :native) :: integer()

Returns the current monotonic time in the given time unit.

This time is monotonically increasing and starts in an unspecified point in time.

Checks if the system will halt or not at the end of ARGV processing.

Marks if the system should halt or not at the end of ARGV processing.

Returns the current operating system (OS) time.

The result is returned in the :native time unit.

This time may be adjusted forwards or backwards in time with no limitation and is not monotonic.

Inlined by the compiler.

@spec os_time(time_unit() | :native) :: integer()

Returns the current operating system (OS) time in the given time unit.

This time may be adjusted forwards or backwards in time with no limitation and is not monotonic.

Returns the Erlang/OTP release number.

Returns the operating system PID for the current Erlang runtime system instance.

Returns a string containing the (usually) numerical identifier for a process. On Unix-like operating systems, this is typically the return value of the getpid() system call. On Windows, the process ID as returned by the GetCurrentProcessId() system call is used.

Examples

System.pid()

Sets multiple environment variables.

Sets a new value for each environment variable corresponding to each {key, value} pair in enum. Keys and non-nil values are automatically converted to charlists. nil values erase the given keys.

Overall, this is a convenience wrapper around put_env/2 anddelete_env/2 with support for different key and value formats.

Sets an environment variable value.

Sets a new value for the environment variable varname.

Restarts all applications in the Erlang runtime system.

All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system starts all applications once again.

Examples

System.restart()

Returns the number of schedulers in the VM.

Returns the number of schedulers online in the VM.

Executes the given command in the OS shell.

It uses sh for Unix-like systems and cmd for Windows.

Watch out

Use this function with care. In particular, never pass untrusted user input to this function, as the user would be able to perform "command injection attacks" by executing any code directly on the machine. Generally speaking, prefer to use cmd/3over this function.

Examples

iex> System.shell("echo hello")
{"hello\n", 0}

If you want to stream the output to Standard IO as it arrives:

iex> System.shell("echo hello", into: IO.stream())
hello
{%IO.Stream{}, 0}

Options

It accepts the same options as cmd/3 (except for arg0). It also accepts the following exclusive options:

This function is deprecated. Use __STACKTRACE__ instead.

Deprecated mechanism to retrieve the last exception stacktrace.

It always return an empty list.

Asynchronously and carefully stops the Erlang runtime system.

All applications are taken down smoothly, all code is unloaded, and all ports are closed before the system terminates by calling halt/1.

status must be a non-negative integer or a binary.

Note this function is asynchronous and the current process will continue executing after this function is invoked. In case you want to block the current process until the system effectively shuts down, you can invokeProcess.sleep(:infinity).

Examples

System.stop(0)
System.stop(1)

Returns the current system time in the :native time unit.

It is the VM view of the os_time/0. They may not match in case of time warps although the VM works towards aligning them. This time is not monotonic.

Inlined by the compiler.

@spec system_time(time_unit() | :native) :: integer()

Returns the current system time in the given time unit.

It is the VM view of the os_time/0. They may not match in case of time warps although the VM works towards aligning them. This time is not monotonic.

Returns the current time offset between the Erlang VM monotonic time and the Erlang VM system time.

The result is returned in the :native time unit.

See time_offset/1 for more information.

Inlined by the compiler.

@spec time_offset(time_unit() | :native) :: integer()

Returns the current time offset between the Erlang VM monotonic time and the Erlang VM system time.

The result is returned in the given time unit unit. The returned offset, added to an Erlang monotonic time (for instance, one obtained withmonotonic_time/1), gives the Erlang system time that corresponds to that monotonic time.

Writable temporary directory.

Returns a writable temporary directory. Searches for directories in the following order:

  1. the directory named by the TMPDIR environment variable
  2. the directory named by the TEMP environment variable
  3. the directory named by the TMP environment variable
  4. C:\TMP on Windows or /tmp on Unix-like operating systems
  5. 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 RuntimeErrorinstead of returning nil if no temp dir is set.

@spec trap_signal(signal(), id, (-> :ok)) :: {:ok, id} | {:error, :already_registered} | {:error, :not_sup} when id: term()

Traps the given signal to execute the fun.

Avoid setting traps in libraries

Trapping signals may have strong implications on how a system shuts down and behaves in production and therefore it is extremely discouraged for libraries to set their own traps. Instead, they should redirect users to configure them themselves. The only cases where it is acceptable for libraries to set their own traps is when using Elixir in script mode, such as in .exs files and via Mix tasks.

An optional id that uniquely identifies the function can be given, otherwise a unique one is automatically generated. If a previously registered id is given, this function returns an error tuple. The id can be used to remove a registered signal by callinguntrap_signal/2.

The given fun receives no arguments and it must return:ok.

It returns {:ok, id} in case of success,{:error, :already_registered} in case the id has already been registered for the given signal, or {:error, :not_sup}in case trapping exists is not supported by the current OS.

The first time a signal is trapped, it will override the default behavior from the operating system. If the same signal is trapped multiple times, subsequent functions given to trap_signal will execute first. In other words, you can consider each function is prepended to the signal handler.

By default, the Erlang VM register traps to the three signals:

Therefore, if you add traps to the signals above, the default behavior above will be executed after all user signals.

Implementation notes

All signals run from a single process. Therefore, blocking thefun will block subsequent traps. It is also not possible to add or remove traps from within a trap itself.

Internally, this functionality is built on top of :os.set_signal/2. When you register a trap, Elixir automatically sets it to :handleand it reverts it back to :default once all traps are removed (except for :sigquit, :sigterm, and :sigusr1 which are always handled). If you or a library call :os.set_signal/2 directly, it may disable Elixir traps (or Elixir may override your configuration).

@spec unique_integer([:positive | :monotonic]) :: integer()

Generates and returns an integer that is unique in the current runtime instance.

"Unique" means that this function, called with the same list of modifiers, will never return the same integer more than once on the current runtime instance.

If modifiers is [], then a unique integer (that can be positive or negative) is returned. Other modifiers can be passed to change the properties of the returned integer:

All modifiers listed above can be combined; repeated modifiers in modifierswill be ignored.

Inlined by the compiler.

@spec untrap_signal(signal(), id) :: :ok | {:error, :not_found} when id: term()

Removes a previously registered signal with id.

User home directory.

Returns the user home directory (platform independent).

User home directory, exception on error.

Same as user_home/0 but raises RuntimeErrorinstead of returning nil if no user home is set.

Elixir version information.

Returns Elixir's version as binary.