Skip to content

OS module

Francesco Saverio Castellano edited this page Mar 18, 2020 · 24 revisions

OS.exec(array command, [bool close])
Executes the command whose arguments are specified in the array and returns the resulting output and exit code.
The command array must contains the full path of the command and all arguments that must be passed to it.
When the optional argument close is provided and set to true, this call immediately returns after executing the command, without reading the output.
The return value is an object with two attributes:

  • result: an integer whose value is the exit status of the command(0 when the command is successful and 1 when the command failed to execute)
  • output: the command output as a string. This attribute is only present if the close argument is not set to true
    The following examples executes grep nginx to get the PID of the nginx process:
var pid = OS.exec(["/usr/bin/pgrep", "nginx"]).output;

 
OS.filesize(string path)
Returns the size of the file in bytes or -1 is the file is not found.  
 
OS.lastModifiedTime(string path)
Returns the last modified time of the file specified by path as a UNIX timestamp.  
 
OS.unlink(string path)
Removes the file specified in path. Returns 0 on success and -1 on failure.  
 
OS.mkdir(string path)
Creates the directory specified in path or returns -1 on failures.  
 
OS.rmdir(string path)
Removes the directory specified in path. Returns 0 on success and -1 on failure.  
 
OS.mkpath(string path)
Creates the directory three specified in path. Returns 0 on success and -1 on failure.  
 
OS.listDir(string path, [string ext])
Returns an array with all files in the directory specified in path. If ext is given then only the files ending with ext are returned.  
 
OS.isFileAndReadable(string path)
Returns true is the file specified in path exists and is readable (path exists and it is a file).  
 
OS.isDirAndReadable(string path)
Returns true is the directory scpecified path exists and is readable (path exists and it is a directory).    
OS.readFile(string path)
Returns the file specified in path and returns its content in a string.  
 
OS.writeFile(string path, string content)
Writes content into the file specified in path.  
 
OS.fopen(string path, string mode)
Opens the file specified in path with the requested access mode. The access mode accepts the same flags as the C fopen function ('r' for reading, 'w' for writing, 'r+' for read and write, etc.).
Returns an integer that represents the file descriptor.
The following examples shows how to open a file for read and write:

var fd = OS.fopen("file.txt", "r+");

 
OS.fseek(integer fd, integer offset)
Sets the internal write/read cursor of the file descriptor identified by fd to the specified offset.
After executing seek all subsequent read/write operations are relative to the offset that was set.
 
 
OS.writeBytes(integer fd, array bytes)
Write bytes to the file descriptor identified by fd.
The following example shows how to open a binary file for writing and writing an array of bytes to it:

var fd = OS.fopen("file.txt", "wb");
OS.writeBytes(fd, [5, 10, 20]);

 
OS.readBytes(integer fd, integer count)
Reads count bytes from the file descriptor identified by fd and returns them in an array.
Depending on the current offset, the size of the file and the value of count the number of bytes returned in the array might be lower than the requested count if the end of the file is reached.  
 
OS.writeString(integer fd, string text)
Writes text to the file descriptor identified by fd.
 
 
OS.readString(integer fd, integer count)
Reads a string of length count from the file descriptor identified by fd.
Depending on the current offset, the size of the file and the value of count the length of the string returned might be lower than the requested count if the end of the file is reached.  
 
OS.fclose(integer fd)
Closes the file identified by the file descriptor fd.
 
 
OS.cwd()
Returns the current-working-directory.  
 
OS.gethostname()
Returns the hostname of the current machine.  
 
OS.sleep(integer milliseconds)
Pauses the execution of the current worker for the requested amount of milliseconds.  
 
OS.getEnv(string name, string defaultValue)
Gets the value of the environment variable identified by name and returns its value as a string.
If defaultValue is provided then when no such variable is found defaultValue is returned.  
 
OS.setEnv(string name, string value)
Sets the value of the environment variable identified by name.  
 

Clone this wiki locally