Erlang -- os

Reference Manual
Release Notes
PDF
Top

Kernel
Reference Manual
Version 5.0

Expand All
Contract All

Table of Contents

- kernel (App)
- application
  - Top of manual page
  - ensure_all_started/1
  - ensure_all_started/2
  - ensure_started/1
  - ensure_started/2
  - get_all_env/0
  - get_all_env/1
  - get_all_key/0
  - get_all_key/1
  - get_application/0
  - get_application/1
  - get_env/1
  - get_env/2
  - get_env/3
  - get_key/1
  - get_key/2
  - load/1
  - load/2
  - loaded_applications/0
  - permit/2
  - set_env/3
  - set_env/4
  - start/1
  - start/2
  - start_type/0
  - stop/1
  - takeover/2
  - unload/1
  - unset_env/2
  - unset_env/3
  - which_applications/0
  - which_applications/1
  - Module:config_change/3
  - Module:prep_stop/1
  - Module:start/2
  - Module:start_phase/3
  - Module:stop/1
- auth
  - Top of manual page
  - cookie/0
  - cookie/1
  - is_auth/1
  - node_cookie/2
  - node_cookie/1
- code
  - Top of manual page
  - add_path/1
  - add_patha/1
  - add_paths/1
  - add_pathsa/1
  - add_pathsz/1
  - add_pathz/1
  - all_loaded/0
  - atomic_load/1
  - clash/0
  - compiler_dir/0
  - del_path/1
  - delete/1
  - ensure_loaded/1
  - ensure_modules_loaded/1
  - finish_loading/1
  - get_mode/0
  - get_object_code/1
  - get_path/0
  - is_loaded/1
  - is_module_native/1
  - is_sticky/1
  - lib_dir/0
  - lib_dir/1
  - lib_dir/2
  - load_abs/1
  - load_binary/3
  - load_file/1
  - objfile_extension/0
  - prepare_loading/1
  - priv_dir/1
  - purge/1
  - replace_path/2
  - root_dir/0
  - set_path/1
  - soft_purge/1
  - stick_dir/1
  - unstick_dir/1
  - where_is_file/1
  - which/1
- disk_log
  - Top of manual page
  - accessible_logs/0
  - alog/2
  - alog_terms/2
  - balog/2
  - balog_terms/2
  - bchunk/2
  - bchunk/3
  - block/1
  - block/2
  - blog/2
  - blog_terms/2
  - breopen/3
  - btruncate/2
  - change_header/2
  - change_notify/3
  - change_size/2
  - chunk/2
  - chunk/3
  - chunk_info/1
  - chunk_step/3
  - close/1
  - format_error/1
  - inc_wrap_file/1
  - info/1
  - lclose/1
  - lclose/2
  - log/2
  - log_terms/2
  - open/1
  - pid2name/1
  - reopen/2
  - reopen/3
  - sync/1
  - truncate/1
  - truncate/2
  - unblock/1
- erl_boot_server
  - Top of manual page
  - add_slave/1
  - delete_slave/1
  - start/1
  - start_link/1
  - which_slaves/0
- erl_ddll
  - Top of manual page
  - demonitor/1
  - format_error/1
  - info/0
  - info/1
  - info/2
  - load/2
  - load_driver/2
  - loaded_drivers/0
  - monitor/2
  - reload/2
  - reload_driver/2
  - try_load/3
  - try_unload/2
  - unload/1
  - unload_driver/1
- erl_prim_loader
- erlang
- error_handler
  - Top of manual page
  - raise_undef_exception/3
  - undefined_function/3
  - undefined_lambda/3
- error_logger
  - Top of manual page
  - add_report_handler/1
  - add_report_handler/2
  - delete_report_handler/1
  - error_msg/1
  - error_msg/2
  - error_report/1
  - error_report/2
  - format/2
  - info_msg/1
  - info_msg/2
  - info_report/1
  - info_report/2
  - logfile/1
  - tty/1
  - warning_map/0
  - warning_msg/1
  - warning_msg/2
  - warning_report/1
  - warning_report/2
- file
  - Top of manual page
  - advise/4
  - allocate/3
  - change_group/2
  - change_mode/2
  - change_owner/2
  - change_owner/3
  - change_time/2
  - change_time/3
  - close/1
  - consult/1
  - copy/2
  - copy/3
  - datasync/1
  - del_dir/1
  - delete/1
  - eval/1
  - eval/2
  - format_error/1
  - get_cwd/0
  - get_cwd/1
  - list_dir/1
  - list_dir_all/1
  - make_dir/1
  - make_link/2
  - make_symlink/2
  - native_name_encoding/0
  - open/2
  - path_consult/2
  - path_eval/2
  - path_open/3
  - path_script/2
  - path_script/3
  - pid2name/1
  - position/2
  - pread/2
  - pread/3
  - pwrite/2
  - pwrite/3
  - read/2
  - read_file/1
  - read_file_info/1
  - read_file_info/2
  - read_line/1
  - read_link/1
  - read_link_all/1
  - read_link_info/1
  - read_link_info/2
  - rename/2
  - script/1
  - script/2
  - sendfile/2
  - sendfile/5
  - set_cwd/1
  - sync/1
  - truncate/1
  - write/2
  - write_file/2
  - write_file/3
  - write_file_info/2
  - write_file_info/3
- gen_sctp
  - Top of manual page
  - abort/2
  - close/1
  - connect/4
  - connect/5
  - connect_init/4
  - connect_init/5
  - controlling_process/2
  - eof/2
  - error_string/1
  - listen/2
  - open/0
  - open/1
  - open/2
  - peeloff/2
  - recv/1
  - recv/2
  - send/3
  - send/4
- gen_tcp
  - Top of manual page
  - accept/1
  - accept/2
  - close/1
  - connect/3
  - connect/4
  - controlling_process/2
  - listen/2
  - recv/2
  - recv/3
  - send/2
  - shutdown/2
- gen_udp
  - Top of manual page
  - close/1
  - controlling_process/2
  - open/1
  - open/2
  - recv/2
  - recv/3
  - send/4
- global
  - Top of manual page
  - del_lock/1
  - del_lock/2
  - notify_all_name/3
  - random_exit_name/3
  - random_notify_name/3
  - re_register_name/2
  - re_register_name/3
  - register_name/2
  - register_name/3
  - registered_names/0
  - send/2
  - set_lock/1
  - set_lock/2
  - set_lock/3
  - sync/0
  - trans/2
  - trans/3
  - trans/4
  - unregister_name/1
  - whereis_name/1
- global_group
  - Top of manual page
  - global_groups/0
  - info/0
  - monitor_nodes/1
  - own_nodes/0
  - registered_names/1
  - send/2
  - send/3
  - sync/0
  - whereis_name/1
  - whereis_name/2
- heart
  - Top of manual page
  - clear_callback/0
  - clear_cmd/0
  - get_callback/0
  - get_cmd/0
  - get_options/0
  - set_callback/2
  - set_cmd/1
  - set_options/1
- inet
  - Top of manual page
  - close/1
  - format_error/1
  - get_rc/0
  - getaddr/2
  - getaddrs/2
  - gethostbyaddr/1
  - gethostbyname/1
  - gethostbyname/2
  - gethostname/0
  - getifaddrs/0
  - getopts/2
  - getstat/1
  - getstat/2
  - ntoa/1
  - parse_address/1
  - parse_ipv4_address/1
  - parse_ipv4strict_address/1
  - parse_ipv6_address/1
  - parse_ipv6strict_address/1
  - parse_strict_address/1
  - peername/1
  - peernames/1
  - peernames/2
  - port/1
  - setopts/2
  - sockname/1
  - socknames/1
  - socknames/2
- inet_res
  - Top of manual page
  - getbyname/2
  - getbyname/3
  - gethostbyaddr/1
  - gethostbyaddr/2
  - gethostbyname/1
  - gethostbyname/2
  - gethostbyname/3
  - lookup/3
  - lookup/4
  - lookup/5
  - nnslookup/4
  - nnslookup/5
  - nslookup/3
  - nslookup/4
  - resolve/3
  - resolve/4
  - resolve/5
- init
- net_adm
  - Top of manual page
  - dns_hostname/1
  - host_file/0
  - localhost/0
  - names/0
  - names/1
  - ping/1
  - world/0
  - world/1
  - world_list/1
  - world_list/2
- net_kernel
  - Top of manual page
  - allow/1
  - connect_node/1
  - get_net_ticktime/0
  - monitor_nodes/1
  - monitor_nodes/2
  - set_net_ticktime/1
  - set_net_ticktime/2
  - start/1
  - start/1
  - start/1
  - stop/0
- os
  - Top of manual page
  - cmd/1
  - find_executable/1
  - find_executable/2
  - getenv/0
  - getenv/1
  - getenv/2
  - getpid/0
  - perf_counter/0
  - perf_counter/1
  - putenv/2
  - system_time/0
  - system_time/1
  - timestamp/0
  - type/0
  - unsetenv/1
  - version/0
- pg2
  - Top of manual page
  - create/1
  - delete/1
  - get_closest_pid/1
  - get_local_members/1
  - get_members/1
  - join/2
  - leave/2
  - start/0
  - start_link/0
  - which_groups/0
- rpc
  - Top of manual page
  - abcast/2
  - abcast/3
  - async_call/4
  - block_call/4
  - block_call/5
  - call/4
  - call/5
  - cast/4
  - eval_everywhere/3
  - eval_everywhere/4
  - multi_server_call/2
  - multi_server_call/3
  - multicall/3
  - multicall/4
  - multicall/5
  - nb_yield/1
  - nb_yield/2
  - parallel_eval/1
  - pinfo/1
  - pinfo/2
  - pmap/3
  - sbcast/2
  - sbcast/3
  - server_call/4
  - yield/1
- seq_trace
  - Top of manual page
  - get_system_tracer/0
  - get_token/0
  - get_token/1
  - print/1
  - print/2
  - reset_trace/0
  - set_system_tracer/1
  - set_token/1
  - set_token/2
- user
- wrap_log_reader
  - Top of manual page
  - chunk/1
  - chunk/2
  - close/1
  - open/1
  - open/2
- zlib
- app
- config

# os

### MODULE

os

### MODULE SUMMARY

Operating system-specific functions.

### DESCRIPTION

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.

### EXPORTS

cmd(Command) -> string()

Types:

Command = atom() | io_lib:chars()

Executes Command in a command shell of the target OS, captures the standard output of the command, and returns this result as a string. This function is a replacement of the previous function unix:cmd/1; they are equivalent on a Unix platform.

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.

find_executable(Name) -> Filename | false
find_executable(Name, Path) -> Filename | false

Types:

Name = Path = Filename = string()

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.

getenv() -> [string()]

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 erlmanual page), the strings can contain characters with codepoints > 255.

getenv(VarName) -> Value | false

Types:

VarName = Value = string()

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 erlmanual page), the strings VarName and Value can contain characters with codepoints > 255.

getenv(VarName, DefaultValue) -> Value

Types:

VarName = DefaultValue = Value = string()

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 erlmanual page), the strings VarName and Value can contain characters with codepoints > 255.

getpid() -> Value

Types:

Value = string()

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.

putenv(VarName, Value) -> true

Types:

VarName = Value = string()

Sets a new Value for environment variable VarName.

If Unicode filename encoding is in effect (see the erlmanual 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.

system_time() -> integer()

Returns the current OS system time in native time unit.

Note

This time is not a monotonically increasing time.

system_time(Unit) -> integer()

Types:

Unit = erlang:time_unit()

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).

Note

This time is not a monotonically increasing time.

timestamp() -> Timestamp

Types:

Timestamp = erlang:timestamp()
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.

perf_counter() -> Counter

Types:

Counter = integer()

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

perf_counter(Unit) -> integer()

Types:

Unit = erlang:time_unit()

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

type() -> {Osfamily, Osname}

Types:

Osfamily = unix | win32
Osname = atom()

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.

Note

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.

unsetenv(VarName) -> true

Types:

VarName = string()

Deletes the environment variable VarName.

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

version() -> VersionString | {Major, Minor, Release}

Types:

VersionString = string()
Major = Minor = Release = integer() >= 0

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.

Note

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