1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>1997</year><year>2018</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
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.
</legalnotice>
<title>os</title>
<prepared></prepared>
<docno></docno>
<date></date>
<rev></rev>
</header>
<module>os</module>
<modulesummary>Operating system-specific functions.</modulesummary>
<description>
<p>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.</p>
<note>
<p>
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 <em>rejected</em> and will cause primitive
file operations to fail.
</p>
<p>
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
<em>rejected</em> and will cause environment variable
operations to fail.
</p>
</note>
</description>
<datatypes>
<datatype>
<name name="env_var_name"/>
<desc>
<p>A string containing valid characters on the specific
OS for environment variable names using
<seealso marker="file#native_name_encoding/0"><c>file:native_name_encoding()</c></seealso>
encoding. Note that specifically null characters (integer
value zero) and <c>$=</c> 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.
</p>
</desc>
</datatype>
<datatype>
<name name="env_var_value"/>
<desc>
<p>A string containing valid characters on the specific
OS for environment variable values using
<seealso marker="file#native_name_encoding/0"><c>file:native_name_encoding()</c></seealso>
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.
</p>
</desc>
</datatype>
<datatype>
<name name="env_var_name_value"/>
<desc>
<p>
Assuming that environment variables has been correctly
set, a strings containing valid characters on the specific
OS for environment variable names and values using
<seealso marker="file#native_name_encoding/0"><c>file:native_name_encoding()</c></seealso>
encoding. The first <c>$=</c> characters appearing in
the string separates environment variable name (on the
left) from environment variable value (on the right).
</p>
</desc>
</datatype>
<datatype>
<name name="os_command"/>
<desc>
<p>All characters needs to be valid characters on the
specific OS using
<seealso marker="file#native_name_encoding/0"><c>file:native_name_encoding()</c></seealso>
encoding. Note that specifically null characters (integer
value zero) are not allowed. However, note that not all
invalid characters not necessarily will cause
<seealso marker="#cmd/1"><c>os:cmd/1</c></seealso>
to fail, but may instead produce invalid results.
</p>
</desc>
</datatype>
<datatype>
<name name="os_command_opts"/>
<desc>
<p>Options for <seealso marker="#cmd/2"><c>os:cmd/2</c></seealso></p>
<taglist>
<tag><c>max_size</c></tag>
<item>
<p>The maximum size of the data returned by the <c>os:cmd</c> call.
See the <seealso marker="#cmd/2"><c>os:cmd/2</c></seealso>
documentation for more details.</p>
</item>
</taglist>
</desc>
</datatype>
</datatypes>
<funcs>
<func>
<name name="cmd" arity="1"/>
<name name="cmd" arity="2" since="OTP 20.2.3"/>
<fsummary>Execute a command in a shell of the target OS.</fsummary>
<desc>
<p>Executes <c><anno>Command</anno></c> in a command shell of the
target OS, captures the standard output of the command,
and returns this result as a string.</p>
<warning><p>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.</p></warning>
<p><em>Examples:</em></p>
<code type="none">
LsOut = os:cmd("ls"), % on unix platform
DirOut = os:cmd("dir"), % on Win32 platform</code>
<p>Notice that in some cases, standard output of a command when
called from another program (for example, <c>os:cmd/1</c>)
can differ, compared with the standard output of the command
when called directly from an OS command shell.</p>
<p><c>os:cmd/2</c> 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 <c>os:cmd</c>. The possible options are:
</p>
<taglist>
<tag><c>max_size</c></tag>
<item>
<p>The maximum size of the data returned by the <c>os:cmd</c> call.
This option is a safety feature that should be used when the command
executed can return a very large, possibly infinite, result.</p>
<code type="none">
> 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]</code>
</item>
</taglist>
</desc>
</func>
<func>
<name name="find_executable" arity="1"/>
<name name="find_executable" arity="2"/>
<fsummary>Absolute filename of a program.</fsummary>
<desc>
<p>These two functions look up an executable program, with the
specified name and a search path, in the same way as the underlying
OS. <c>find_executable/1</c> uses the current
execution path (that is, the environment variable <c>PATH</c> on
Unix and Windows).</p>
<p><c><anno>Path</anno></c>, if specified, is to conform to the syntax
of execution paths on the OS. Returns the absolute filename of the
executable program <c><anno>Name</anno></c>,
or <c>false</c> if the program is not found.</p>
</desc>
</func>
<func>
<name name="getenv" arity="0"/>
<fsummary>List all environment variables.</fsummary>
<desc>
<p>Returns a list of all environment variables.
Each environment variable is expressed as a single string on
the format <c>"VarName=Value"</c>, where <c>VarName</c> is
the name of the variable and <c>Value</c> its value.</p>
<p>If Unicode filename encoding is in effect (see the
<seealso marker="erts:erl#file_name_encoding"><c>erl</c> manual
page</seealso>), the strings can contain characters with
codepoints > 255.</p>
</desc>
</func>
<func>
<name name="getenv" arity="1"/>
<fsummary>Get the value of an environment variable.</fsummary>
<desc>
<p>Returns the <c><anno>Value</anno></c> of the environment variable
<c><anno>VarName</anno></c>, or <c>false</c> if the environment
variable is undefined.</p>
<p>If Unicode filename encoding is in effect (see the
<seealso marker="erts:erl#file_name_encoding"><c>erl</c> manual
page</seealso>), the strings <c><anno>VarName</anno></c> and
<c><anno>Value</anno></c> can contain characters with
codepoints > 255.</p>
</desc>
</func>
<func>
<name name="getenv" arity="2" since="OTP 18.0"/>
<fsummary>Get the value of an environment variable.</fsummary>
<desc>
<p>Returns the <c><anno>Value</anno></c> of the environment variable
<c><anno>VarName</anno></c>, or <c>DefaultValue</c> if the
environment variable is undefined.</p>
<p>If Unicode filename encoding is in effect (see the
<seealso marker="erts:erl#file_name_encoding"><c>erl</c> manual
page</seealso>), the strings <c><anno>VarName</anno></c> and
<c><anno>Value</anno></c> can contain characters with
codepoints > 255.</p>
</desc>
</func>
<func>
<name name="getpid" arity="0"/>
<fsummary>Return the process identifier of the emulator
process.</fsummary>
<desc>
<p>Returns the process identifier of the current Erlang emulator
in the format most commonly used by the OS environment.
Returns <c><anno>Value</anno></c> as a string containing
the (usually) numerical identifier for a process. On Unix,
this is typically the return value of the <c>getpid()</c>
system call. On Windows,
the process id as returned by the <c>GetCurrentProcessId()</c>
system call is used.</p>
</desc>
</func>
<func>
<name name="putenv" arity="2"/>
<fsummary>Set a new value for an environment variable.</fsummary>
<desc>
<p>Sets a new <c><anno>Value</anno></c> for environment variable
<c><anno>VarName</anno></c>.</p>
<p>If Unicode filename encoding is in effect (see the
<seealso marker="erts:erl#file_name_encoding"><c>erl</c> manual
page</seealso>), the strings <c><anno>VarName</anno></c> and
<c><anno>Value</anno></c> can contain characters with
codepoints > 255.</p>
<p>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.</p>
<note>
<p>
<c><anno>VarName</anno></c> is not allowed to contain
an <c>$=</c> character. Previous implementations used
to just let the <c>$=</c> character through which
silently caused erroneous results. Current implementation
will instead throw a <c>badarg</c> exception.
</p>
</note>
</desc>
</func>
<func>
<name name="set_signal" arity="2" since="OTP 20.0"/>
<fsummary>Enables or disables handling of OS signals.</fsummary>
<desc>
<p>Enables or disables OS signals.</p>
<p>Each signal my be set to one of the following options:</p>
<taglist>
<tag><c>ignore</c></tag>
<item>
This signal will be ignored.
</item>
<tag><c>default</c></tag>
<item>
This signal will use the default signal handler for the operating system.
</item>
<tag><c>handle</c></tag>
<item>
This signal will notify
<seealso marker="kernel_app#erl_signal_server"><c>erl_signal_server</c></seealso>
when it is received by the Erlang runtime system.
</item>
</taglist>
</desc>
</func>
<func>
<name name="system_time" arity="0" since="OTP 18.0"/>
<fsummary>Current OS system time.</fsummary>
<desc>
<p>Returns the current
<seealso marker="erts:time_correction#OS_System_Time">OS system time</seealso>
in <c>native</c>
<seealso marker="erts:erlang#type_time_unit">time unit</seealso>.</p>
<note><p>This time is <em>not</em> a monotonically increasing time.</p>
</note>
</desc>
</func>
<func>
<name name="system_time" arity="1" since="OTP 18.0"/>
<fsummary>Current OS system time.</fsummary>
<desc>
<p>Returns the current
<seealso marker="erts:time_correction#OS_System_Time">OS system time</seealso>
converted into the <c><anno>Unit</anno></c> passed as argument.</p>
<p>Calling <c>os:system_time(<anno>Unit</anno>)</c> is equivalent to
<seealso marker="erts:erlang#convert_time_unit/3"><c>erlang:convert_time_unit</c></seealso>(<seealso marker="#system_time/0"><c>os:system_time()</c></seealso><c>,
native, <anno>Unit</anno>)</c>.</p>
<note><p>This time is <em>not</em> a monotonically increasing time.</p>
</note>
</desc>
</func>
<func>
<name name="timestamp" arity="0"/>
<fsummary>Current OS system time on the <c>erlang:timestamp/0</c> format.</fsummary>
<type_desc variable="Timestamp">Timestamp = {MegaSecs, Secs, MicroSecs}</type_desc>
<desc>
<p>Returns the current
<seealso marker="erts:time_correction#OS_System_Time">OS system time</seealso>
in the same format as
<seealso marker="erts:erlang#timestamp/0"><c>erlang:timestamp/0</c></seealso>.
The tuple can be used together with function
<seealso marker="stdlib:calendar#now_to_universal_time/1"><c>calendar:now_to_universal_time/1</c></seealso>
or <seealso marker="stdlib:calendar#now_to_local_time/1"><c>calendar:now_to_local_time/1</c></seealso>
to get calendar time. Using the calendar time, together with the
<c>MicroSecs</c> 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.</p>
<p>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:</p>
<code>
-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]).</code>
<p>This module can be used as follows:</p>
<pre>
1> <input>io:format("~s~n",[print_time:format_utc_timestamp()]).</input>
29 Apr 2009 9:55:30.051711</pre>
<p>OS system time can also be retreived by
<seealso marker="#system_time/0"><c>system_time/0</c></seealso> and
<seealso marker="#system_time/1"><c>system_time/1</c></seealso>.</p>
</desc>
</func>
<func>
<name name="perf_counter" arity="0" since="OTP 19.0"/>
<fsummary>Returns a performance counter</fsummary>
<desc>
<p>Returns the current performance counter value in <c>perf_counter</c>
<seealso marker="erts:erlang#type_time_unit">time unit</seealso>.
This is a highly optimized call that might not be traceable.
</p>
</desc>
</func>
<func>
<name name="perf_counter" arity="1" since="OTP 19.0"/>
<fsummary>Returns a performance counter</fsummary>
<desc><p>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.</p>
<pre>1> <input>T1 = os:perf_counter(1000),receive after 10000 -> ok end,T2 = os:perf_counter(1000).</input>
176525861
2> <input>T2 - T1.</input>
10004</pre>
</desc>
</func>
<func>
<name name="type" arity="0"/>
<fsummary>Return the OS family and, in some cases, the OS name of the
current OS.</fsummary>
<desc>
<p>Returns the <c><anno>Osfamily</anno></c> and, in some cases, the
<c><anno>Osname</anno></c> of the current OS.</p>
<p>On Unix, <c><anno>Osname</anno></c> has the same value as
<c>uname -s</c> returns, but in lower case. For example, on
Solaris 1 and 2, it is <c>sunos</c>.</p>
<p>On Windows, <c><anno>Osname</anno></c> is <c>nt</c>.</p>
<note>
<p>Think twice before using this function. Use module
<seealso marker="stdlib:filename"><c>filename</c></seealso>
if you want to inspect or build filenames in a portable way.
Avoid matching on atom <c><anno>Osname</anno></c>.</p>
</note>
</desc>
</func>
<func>
<name name="unsetenv" arity="1" since="OTP R16B03"/>
<fsummary>Delete an environment variable.</fsummary>
<desc>
<p>Deletes the environment variable <c><anno>VarName</anno></c>.</p>
<p>If Unicode filename encoding is in effect (see the
<seealso marker="erts:erl#file_name_encoding"><c>erl</c> manual
page</seealso>), the string <c><anno>VarName</anno></c> can
contain characters with codepoints > 255.</p>
</desc>
</func>
<func>
<name name="version" arity="0"/>
<fsummary>Return the OS versions.</fsummary>
<desc>
<p>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.</p>
<note>
<p>Think twice before using this function. If you still need
to use it, always <c>call os:type()</c> first.</p>
</note>
</desc>
</func>
</funcs>
</erlref>
|