os¶
This module exposes low level operating system facilities / syscalls.
Functions¶
-
os.
chdir
(path)¶ Changes the current working directory of the calling process to the directory specified in path.
See also
-
os.
cloexec
(fd, set)¶ Sets or clears the
O_CLOEXEC
flag on the given fd. Since version 0.3.0 all fds are created withO_CLOEXEC
set.
-
os.
dup
(oldfd)¶ Duplicates the given file descriptor, returning the lowest available one.
See also
Note
Since version 0.3.0 the fds are created with
O_CLOEXEC
set. You can undo this usingos.cloexec()
.
-
os.
dup2
(oldfd, newfd[, cloexec])¶ Duplicates oldfd into newfd, setting the
O_CLOEXEC
flag if indicated. It defaults totrue
;See also
-
os.
execv
(path[, args])¶
-
os.
execvp
(file[, args])¶
-
os.
execve
(path[, args][, envp])¶
-
os.
execvpe
(file[, args][, envp])¶ The
exec*
family of functions replace the current process image with a new process image.Arguments: - file – file or path indicate file to be used for the new process image. The functions which contain
a
p
in their name will search for the file in thePATH
environment variable, or in the current directory in its absence. - args – Arguments for the program. If an
Array
is passed, the first element should be the program filename. - envp – Object containing the environment for the new program. The functions which do not take ab environment object will inherit it from their parent process.
See also
- file – file or path indicate file to be used for the new process image. The functions which contain
a
-
os.
exit
([status])¶ Ends the process with the specified status. It defaults to 0.
See also
Note
At the moment no clean shutdown is performed.
-
os.
fork
()¶ Creates a new process duplicating the calling process. See
os.waitpid()
for how to wait for the child process.See also
-
os.
getegid
()¶ Returns the effective group ID of the calling process.
See also
-
os.
geteuid
()¶ Returns the effective user ID of the calling process.
See also
-
os.
getgroups
()¶ Returns the supplementary group IDs of the calling process.
See also
-
os:getppid
()¶ Returns the process id of the parent of the calling process.
See also
-
os.
isatty
(fd)¶ Returns
true
if the given fd refers to a valid terminal type device,false
otherwise.See also
-
os.
nonblock
(fd, set)¶ Sets or clears the
O_NONBLOCK
flag on the given fd.
-
os.
open
(path, flags, mode)¶ Opens a file.
Arguments: - path – The file path to be opened.
- flags –
How the file will be opened. It can be a string or an OR-ed mask of constants (listed below). Here are the supported possibilities:
- ’r’ =
O_RDONLY
: open the file just for reading - ’r+’ =
O_RDWR
: open the file for reading and writing - ’w’ =
O_TRUNC | O_CREAT | O_WRONLY
: open the file for writing only, truncating it if it exists and creating it otherwise - ’wx’ =
O_TRUNC | O_CREAT | O_WRONLY | O_EXCL
: like ‘w’, but fails if the path exists - ’w+’ =
O_TRUNC | O_CREAT | O_RDWR
: open the file for reading and writing, truncating it if it exists and creating it otherwise - ’wx+’ =
O_TRUNC | O_CREAT | O_RDWR | O_EXCL
: like ‘w+’ but fails if the path exists - ’a’ =
O_APPEND | O_CREAT | O_WRONLY
: open the file for apending, creating it if it doesn’t exist - ’ax’ =
O_APPEND | O_CREAT | O_WRONLY | O_EXCL
: like ‘a’ but fails if the path exists - ’a+’ =
O_APPEND | O_CREAT | O_RDWR
: open the file for reading and apending, creating it if it doesn’t exist - ’ax+’ =
O_APPEND | O_CREAT | O_RDWR | O_EXCL
: like ‘a+’ but fails if the path exists
- ’r’ =
- mode – Sets the file mode (permissions and sticky bits).
Returns: The opened file descriptor.
See also
Note
Since version 0.3.0 the fds are created with
O_CLOEXEC
set. You can undo this usingos.cloexec()
.
-
os.
pipe
()¶ Creates a pipe (an object that allows unidirectional data flow) and allocates a pair of file descriptors. The first descriptor connects to the read end of the pipe; the second connects to the write end. File descriptors are returned in an array:
[read_fd, write_fd]
.See also
Note
Since version 0.3.0 the fds are created with
O_CLOEXEC
set. You can undo this usingos.cloexec()
.
-
os.
read
([nread])¶ Read data from the file descriptor.
Arguments: - nread – Amount of data to receive. If not specified it defaults to 4096. Alternatively, a Buffer can be passed, and data will be read into it.
Returns: The data that was read as a Uint8Array or the amount of data read as a number, if a Buffer was passed.
See also
-
os.
scandir
(path)¶ Lists all files in the given path.
See also
-
os.
setgroups
(groups)¶ Sets the supplementary group IDs for the calling process.
See also
-
os.
setsid
()¶ Create a new session if the calling process is not a process group leader.
See also
-
os.
stat
(path)¶ Obtain information about the file pointed to by path.
Returns an object with the following properties:
- dev
- mode
- nlink
- uid
- gid
- rdev
- ino
- size
- blksize
- blocks
- flags
- gen
- atime
- mtime
- ctime
- birthtime
The
atime
,mtime
,ctime
andbirthtime
fields are of type Date.See also
-
os.
ttyname
(fd)¶ Returns the related device name of the given fd for which
os.isatty()
istrue
.See also
-
os.
unlink
(path)¶ Unlinks (usually this means completely removing) the given path.
See also
-
os.
urandom
(bytes)¶ Get bytes from the system CSPRNG. This is implemented by reading from
/dev/urandom
. On Linux systems supporting the getrandom(2) syscall that one is used, and in OSX arc4random_buf(3).bytes can be an integer or a
Buffer
object. If it’s an integer aBuffer
will be returned of the specified size. If it’s already aBuffer
, if will be filled.
-
os.
waitpid
(pid[, options])¶ Wait for state changes in a child of the calling process. The return value is an object with
pid
andstatus
properties. Theos.W*
family of functions can be used to get more information about the status.See also
-
os.
write
(data)¶ Write data on the file descriptor.
Arguments: - data – The data that will be written (can be a string or a Buffer).
Returns: The number of bytes from data which were actually written.
See also
-
os.
S_ISDIR
(mode)¶ Returns
true
if the mode of the file indicates it’s a directory.
-
os.
S_ISCHR
(mode)¶ Returns
true
if the mode of the file indicates it’s a character device.
-
os.
S_ISBLK
(mode)¶ Returns
true
if the mode of the file indicates it’s a block device.
-
os.
S_ISREG
(mode)¶ Returns
true
if the mode of the file indicates it’s a regular file.
-
os.
S_ISFIFO
(mode)¶ Returns
true
if the mode of the file indicates it’s a FIFO.
-
os.
S_ISLINK
(mode)¶ Returns
true
if the mode of the file indicates it’s a symbolic link.
-
os.
S_ISSOCK
(mode)¶ Returns
true
if the mode of the file indicates it’s a socket.
-
os.
WIFEXITED
(status)¶
-
os.
WEXITSTATUS
(status)¶
-
os.
WIFSIGNALED
(status)¶
-
os.
WTERMSIG
(status)¶
-
os.
WIFSTOPPED
(status)¶
-
os.
WSTOPSIG
(status)¶
-
os.
WIFCONTINUED
(status)¶ Helper functions to get status information from a child process. See the man page: waitpid(2).
Constants¶
-
os.
W*
¶ Flags used in the options field on
os.waitpid()
.
-
os.
STD{IN,OUT,ERR}_FILENO
¶ Constants represendint default file descriptors for stdio.