1os(3)                      Erlang Module Definition                      os(3)
2
3
4

NAME

6       os - Operating system-specific functions.
7

DESCRIPTION

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

DATA TYPES

15       os_command() = atom() | io_lib:chars()
16
17       os_command_opts() = #{max_size => integer() >= 0 | infinity}
18
19              Options for os:cmd/2
20
21                max_size:
22                  The maximum size of the data returned by  the  os:cmd  call.
23                  See the os:cmd/2 documentation for more details.
24

EXPORTS

26       cmd(Command) -> string()
27
28       cmd(Command, Options) -> string()
29
30              Types:
31
32                 Command = os_command()
33                 Options = os_command_opts()
34
35              Executes  Command  in a command shell of the target OS, captures
36              the standard output of the command, and returns this result as a
37              string.
38
39              Examples:
40
41              LsOut = os:cmd("ls"), % on unix platform
42              DirOut = os:cmd("dir"), % on Win32 platform
43
44              Notice  that  in  some  cases, standard output of a command when
45              called from another program (for example, os:cmd/1) can  differ,
46              compared  with  the  standard  output of the command when called
47              directly from an OS command shell.
48
49              os:cmd/2 was added in kernel-5.5 (OTP-20.2.1). It makes it  pos‐
50              sible  to pass an options map as the second argument in order to
51              control the behaviour of os:cmd. The possible options are:
52
53                max_size:
54                  The maximum size of the data returned by  the  os:cmd  call.
55                  This option is a safety feature that should be used when the
56                  command executed can return a very large, possibly infinite,
57                  result.
58
59                > os:cmd("cat /dev/zero", #{ max_size => 20 }).
60                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
61
62       find_executable(Name) -> Filename | false
63
64       find_executable(Name, Path) -> Filename | false
65
66              Types:
67
68                 Name = Path = Filename = string()
69
70              These  two  functions  look  up  an executable program, with the
71              specified name and a search path, in the same way as the  under‐
72              lying  OS.  find_executable/1  uses  the  current execution path
73              (that is, the environment variable PATH on Unix and Windows).
74
75              Path, if specified, is to conform to  the  syntax  of  execution
76              paths on the OS. Returns the absolute filename of the executable
77              program Name, or false if the program is not found.
78
79       getenv() -> [string()]
80
81              Returns a list of all environment  variables.  Each  environment
82              variable  is  expressed  as  a single string on the format "Var‐
83              Name=Value", where VarName is the name of the variable and Value
84              its value.
85
86              If  Unicode  filename  encoding is in effect (see the erl manual
87              page), the strings can contain characters with codepoints > 255.
88
89       getenv(VarName) -> Value | false
90
91              Types:
92
93                 VarName = Value = string()
94
95              Returns the Value of the environment variable VarName, or  false
96              if the environment variable is undefined.
97
98              If  Unicode  filename  encoding is in effect (see the erl manual
99              page), the strings VarName and Value can contain characters with
100              codepoints > 255.
101
102       getenv(VarName, DefaultValue) -> Value
103
104              Types:
105
106                 VarName = DefaultValue = Value = string()
107
108              Returns  the  Value  of  the  environment  variable  VarName, or
109              DefaultValue if the environment variable is undefined.
110
111              If Unicode filename encoding is in effect (see  the  erl  manual
112              page), the strings VarName and Value can contain characters with
113              codepoints > 255.
114
115       getpid() -> Value
116
117              Types:
118
119                 Value = string()
120
121              Returns the process identifier of the current Erlang emulator in
122              the  format  most  commonly  used by the OS environment. Returns
123              Value as a string containing the (usually) numerical  identifier
124              for  a  process.  On Unix, this is typically the return value of
125              the getpid() system call. On Windows, the process id as returned
126              by the GetCurrentProcessId() system call is used.
127
128       putenv(VarName, Value) -> true
129
130              Types:
131
132                 VarName = Value = string()
133
134              Sets a new Value for environment variable VarName.
135
136              If  Unicode  filename  encoding is in effect (see the erl manual
137              page), the strings VarName and Value can contain characters with
138              codepoints > 255.
139
140              On  Unix  platforms, the environment is set using UTF-8 encoding
141              if Unicode filename translation is in effect.  On  Windows,  the
142              environment is set using wide character interfaces.
143
144       set_signal(Signal, Option) -> ok
145
146              Types:
147
148                 Signal =
149                     sighup |
150                     sigquit |
151                     sigabrt |
152                     sigalrm |
153                     sigterm |
154                     sigusr1 |
155                     sigusr2 |
156                     sigchld |
157                     sigstop |
158                     sigtstp
159                 Option = default | handle | ignore
160
161              Enables or disables OS signals.
162
163              Each signal my be set to one of the following options:
164
165                ignore:
166                   This signal will be ignored.
167
168                default:
169                   This  signal  will  use  the default signal handler for the
170                  operating system.
171
172                handle:
173                   This  signal  will  notify  erl_signal_server  when  it  is
174                  received by the Erlang runtime system.
175
176       system_time() -> integer()
177
178              Returns the current OS system time in native time unit.
179
180          Note:
181              This time is not a monotonically increasing time.
182
183
184       system_time(Unit) -> integer()
185
186              Types:
187
188                 Unit = erlang:time_unit()
189
190              Returns  the  current  OS  system  time  converted into the Unit
191              passed as argument.
192
193              Calling  os:system_time(Unit)  is  equivalent   to   erlang:con‐
194              vert_time_unit(os:system_time(), native, Unit).
195
196          Note:
197              This time is not a monotonically increasing time.
198
199
200       timestamp() -> Timestamp
201
202              Types:
203
204                 Timestamp = erlang:timestamp()
205                   Timestamp = {MegaSecs, Secs, MicroSecs}
206
207              Returns  the  current  OS  system  time  in  the  same format as
208              erlang:timestamp/0. The tuple can be used together with function
209              calendar:now_to_universal_time/1 or calendar:now_to_local_time/1
210              to get calendar time. Using the calendar time, together with the
211              MicroSecs  part  of  the return tuple from this function, allows
212              you to log time stamps in high resolution  and  consistent  with
213              the time in the rest of the OS.
214
215              Example  of  code  formatting  a  string  in format "DD Mon YYYY
216              HH:MM:SS.mmmmmm", where DD is the day of month, Mon is the  tex‐
217              tual  month  name,  YYYY  is the year, HH:MM:SS is the time, and
218              mmmmmm is the microseconds in six positions:
219
220              -module(print_time).
221              -export([format_utc_timestamp/0]).
222              format_utc_timestamp() ->
223                  TS = {_,_,Micro} = os:timestamp(),
224                  {{Year,Month,Day},{Hour,Minute,Second}} =
225              calendar:now_to_universal_time(TS),
226                  Mstr = element(Month,{"Jan","Feb","Mar","Apr","May","Jun","Jul",
227                  "Aug","Sep","Oct","Nov","Dec"}),
228                  io_lib:format("~2w ~s ~4w ~2w:~2..0w:~2..0w.~6..0w",
229                  [Day,Mstr,Year,Hour,Minute,Second,Micro]).
230
231              This module can be used as follows:
232
233              1> io:format("~s~n",[print_time:format_utc_timestamp()]).
234              29 Apr 2009  9:55:30.051711
235
236              OS system time can also be retreived by system_time/0  and  sys‐
237              tem_time/1.
238
239       perf_counter() -> Counter
240
241              Types:
242
243                 Counter = integer()
244
245              Returns  the  current  performance counter value in perf_counter
246              time unit. This is a highly optimized call  that  might  not  be
247              traceable.
248
249       perf_counter(Unit) -> integer()
250
251              Types:
252
253                 Unit = erlang:time_unit()
254
255              Returns  a  performance  counter that can be used as a very fast
256              and high resolution timestamp. This  counter  is  read  directly
257              from  the hardware or operating system with the same guarantees.
258              This means that two consecutive calls to the  function  are  not
259              guaranteed  to  be monotonic, though it most likely will be. The
260              performance counter will be converted to the  resolution  passed
261              as an argument.
262
263              1> T1 = os:perf_counter(1000),receive after 10000 -> ok end,T2 = os:perf_counter(1000).
264              176525861
265              2> T2 - T1.
266              10004
267
268       type() -> {Osfamily, Osname}
269
270              Types:
271
272                 Osfamily = unix | win32
273                 Osname = atom()
274
275              Returns  the Osfamily and, in some cases, the Osname of the cur‐
276              rent OS.
277
278              On Unix, Osname has the same value as uname -s returns,  but  in
279              lower case. For example, on Solaris 1 and 2, it is sunos.
280
281              On Windows, Osname is nt.
282
283          Note:
284              Think  twice  before using this function. Use module filename if
285              you want to inspect or build filenames in a portable way.  Avoid
286              matching on atom Osname.
287
288
289       unsetenv(VarName) -> true
290
291              Types:
292
293                 VarName = string()
294
295              Deletes the environment variable VarName.
296
297              If  Unicode  filename  encoding is in effect (see the erl manual
298              page), the string VarName can contain characters with codepoints
299              > 255.
300
301       version() -> VersionString | {Major, Minor, Release}
302
303              Types:
304
305                 VersionString = string()
306                 Major = Minor = Release = integer() >= 0
307
308              Returns the OS version. On most systems, this function returns a
309              tuple, but a string is returned instead if the system  has  ver‐
310              sions that cannot be expressed as three numbers.
311
312          Note:
313              Think twice before using this function. If you still need to use
314              it, always call os:type() first.
315
316
317
318
319Ericsson AB                     kernel 5.4.3.2                           os(3)
Impressum