19972017 Ericsson AB. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. os
os Operating system-specific functions.

The functions in this module are operating system-specific. Careless use of these functions results in programs that will only run on a specific platform. On the other hand, with careful use, these functions can be of help in enabling a program to run on most platforms.

File operations used to accept filenames containing null characters (integer value zero). This caused the name to be truncated and in some cases arguments to primitive operations to be mixed up. Filenames containing null characters inside the filename are now rejected and will cause primitive file operations to fail.

Also environment variable operations used to accept names and values of environment variables containing null characters (integer value zero). This caused operations to silently produce erroneous results. Environment variable names and values containing null characters inside the name or value are now rejected and will cause environment variable operations to fail.

A string containing valid characters on the specific OS for environment variable names using file:native_name_encoding() encoding. Note that specifically null characters (integer value zero) and $= characters are not allowed. However, note that not all invalid characters necessarily will cause the primitiv operations to fail, but may instead produce invalid results.

A string containing valid characters on the specific OS for environment variable values using file:native_name_encoding() encoding. Note that specifically null characters (integer value zero) are not allowed. However, note that not all invalid characters necessarily will cause the primitiv operations to fail, but may instead produce invalid results.

Assuming that environment variables has been correctly set, a strings containing valid characters on the specific OS for environment variable names and values using file:native_name_encoding() encoding. The first $= characters appearing in the string separates environment variable name (on the left) from environment variable value (on the right).

All characters needs to be valid characters on the specific OS using file:native_name_encoding() encoding. Note that specifically null characters (integer value zero) are not allowed. However, note that not all invalid characters not necessarily will cause os:cmd/1 to fail, but may instead produce invalid results.

Options for os:cmd/2

max_size

The maximum size of the data returned by the os:cmd call. See the os:cmd/2 documentation for more details.

Execute a command in a shell of the target OS.

Executes Command in a command shell of the target OS, captures the standard output of the command, and returns this result as a string.

Previous implementation used to allow all characters as long as they were integer values greater than or equal to zero. This sometimes lead to unwanted results since null characters (integer value zero) often are interpreted as string termination. The current implementation rejects these.

Examples:

LsOut = os:cmd("ls"), % on unix platform DirOut = os:cmd("dir"), % on Win32 platform

Notice that in some cases, standard output of a command when called from another program (for example, os:cmd/1) can differ, compared with the standard output of the command when called directly from an OS command shell.

os:cmd/2 was added in kernel-5.5 (OTP-20.2.1). It makes it possible to pass an options map as the second argument in order to control the behaviour of os:cmd. The possible options are:

max_size

The maximum size of the data returned by the os:cmd call. This option is a safety feature that should be used when the command executed can return a very large, possibly infinite, result.

> os:cmd("cat /dev/zero", #{ max_size => 20 }). [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Absolute filename of a program.

These two functions look up an executable program, with the specified name and a search path, in the same way as the underlying OS. find_executable/1 uses the current execution path (that is, the environment variable PATH on Unix and Windows).

Path, if specified, is to conform to the syntax of execution paths on the OS. Returns the absolute filename of the executable program Name, or false if the program is not found.

List all environment variables.

Returns a list of all environment variables. Each environment variable is expressed as a single string on the format "VarName=Value", where VarName is the name of the variable and Value its value.

If Unicode filename encoding is in effect (see the erl manual page), the strings can contain characters with codepoints > 255.

Get the value of an environment variable.

Returns the Value of the environment variable VarName, or false if the environment variable is undefined.

If Unicode filename encoding is in effect (see the erl manual page), the strings VarName and Value can contain characters with codepoints > 255.

Get the value of an environment variable.

Returns the Value of the environment variable VarName, or DefaultValue if the environment variable is undefined.

If Unicode filename encoding is in effect (see the erl manual page), the strings VarName and Value can contain characters with codepoints > 255.

Return the process identifier of the emulator process.

Returns the process identifier of the current Erlang emulator in the format most commonly used by the OS environment. Returns Value as a string containing the (usually) numerical identifier for a process. On Unix, 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.

Set a new value for an environment variable.

Sets a new Value for environment variable VarName.

If Unicode filename encoding is in effect (see the erl manual page), the strings VarName and Value can contain characters with codepoints > 255.

On Unix platforms, the environment is set using UTF-8 encoding if Unicode filename translation is in effect. On Windows, the environment is set using wide character interfaces.

VarName is not allowed to contain an $= character. Previous implementations used to just let the $= character through which silently caused erroneous results. Current implementation will instead throw a badarg exception.

Enables or disables handling of OS signals.

Enables or disables OS signals.

Each signal my be set to one of the following options:

ignore This signal will be ignored. default This signal will use the default signal handler for the operating system. handle This signal will notify erl_signal_server when it is received by the Erlang runtime system.
Current OS system time.

Returns the current OS system time in native time unit.

This time is not a monotonically increasing time.

Current OS system time.

Returns the current OS system time converted into the Unit passed as argument.

Calling os:system_time(Unit) is equivalent to erlang:convert_time_unit(os:system_time(), native, Unit).

This time is not a monotonically increasing time.

Current OS system time on the erlang:timestamp/0 format. Timestamp = {MegaSecs, Secs, MicroSecs}

Returns the current OS system time in the same format as erlang:timestamp/0. The tuple can be used together with function calendar:now_to_universal_time/1 or calendar:now_to_local_time/1 to get calendar time. Using the calendar time, together with the MicroSecs part of the return tuple from this function, allows you to log time stamps in high resolution and consistent with the time in the rest of the OS.

Example of code formatting a string in format "DD Mon YYYY HH:MM:SS.mmmmmm", where DD is the day of month, Mon is the textual month name, YYYY is the year, HH:MM:SS is the time, and mmmmmm is the microseconds in six positions:

-module(print_time). -export([format_utc_timestamp/0]). format_utc_timestamp() -> TS = {_,_,Micro} = os:timestamp(), {{Year,Month,Day},{Hour,Minute,Second}} = calendar:now_to_universal_time(TS), Mstr = element(Month,{"Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep","Oct","Nov","Dec"}), io_lib:format("~2w ~s ~4w ~2w:~2..0w:~2..0w.~6..0w", [Day,Mstr,Year,Hour,Minute,Second,Micro]).

This module can be used as follows:

1> io:format("~s~n",[print_time:format_utc_timestamp()]).
29 Apr 2009  9:55:30.051711

OS system time can also be retreived by system_time/0 and system_time/1.

Returns a performance counter

Returns the current performance counter value in perf_counter time unit. This is a highly optimized call that might not be traceable.

Returns a performance counter

Returns a performance counter that can be used as a very fast and high resolution timestamp. This counter is read directly from the hardware or operating system with the same guarantees. This means that two consecutive calls to the function are not guaranteed to be monotonic, though it most likely will be. The performance counter will be converted to the resolution passed as an argument.

1> T1 = os:perf_counter(1000),receive after 10000 -> ok end,T2 = os:perf_counter(1000).
176525861
2> T2 - T1.
10004
Return the OS family and, in some cases, the OS name of the current OS.

Returns the Osfamily and, in some cases, the Osname of the current OS.

On Unix, Osname has the same value as uname -s returns, but in lower case. For example, on Solaris 1 and 2, it is sunos.

On Windows, Osname is nt.

Think twice before using this function. Use module filename if you want to inspect or build filenames in a portable way. Avoid matching on atom Osname.

Delete an environment variable.

Deletes the environment variable VarName.

If Unicode filename encoding is in effect (see the erl manual page), the string VarName can contain characters with codepoints > 255.

Return the OS versions.

Returns the OS version. On most systems, this function returns a tuple, but a string is returned instead if the system has versions that cannot be expressed as three numbers.

Think twice before using this function. If you still need to use it, always call os:type() first.