Module basic module

Module process

Process processing module to manage the resources of the current process

Reference method:

1
var process = require('process');

Process event

The process module object is an instance of EventEmitter , which can respond to process-level events by registering event listeners.

beforeExit event

When the task of fibjs is empty and no additional work is added, the event beforeExit will be triggered

1
process.on('beforeExit', exitCode => {});

Normally, if no extra work is added to the task queue, the fibjs process will end. But if a new task is started in the callback function of the listener bound to the beforeExit event, such as starting a fiber, then the fibjs process will continue to run.

process.exitCode is passed as the only parameter value to the callback function of the beforeExit event listener. If the process is about to terminate due to explicit reasons, such as calling process.exit directly or throwing an uncaught exception, the beforeExit event will not be triggered.

exit event

When fibjs exits, the exit event will be triggered. Once all the listeners bound to the exit event are executed, the process will terminate

1
process.on('exit', exitCode => {});

The callback function of the exit event listener has only one input parameter. The value of this parameter can be the attribute value of process.exitCode or the exitCode value passed in when the process.exit method is called.

Signal event

When the fibjs process receives a signal, it will trigger a signal event. The currently supported signals are SIGINT and SIGTERM. The name of each event is represented by the uppercase of the signal name (for example, the event'SIGINT' corresponds to the signal SIGINT).

Signal events are different from other process events. Signal events are preempted. When a signal occurs, regardless of the current io operation or JavaScript operation, the corresponding event will be triggered as soon as possible. For example, you can use the following code to interrupt the current application and output the running status:

1 2 3 4 5 6
var coroutine = require('coroutine'); process.on('SIGINT', () => { coroutine.fibers.forEach(f => console.error("Fiber %d:\n%s", f.id, f.stack)); process.exit(); });

The signal names and their meanings are as follows:

  • SIGINT: When the terminal is running, it can be supported by all platforms, and it can usually be triggered by CTRL+C.
  • SIGTERM: This signal is triggered when the process is killed. Not supported under Windows.

Static function

umask

Change the current umask, Windows does not support this method

1
static Integer process.umask(Integer mask);

Call parameters:

  • mask : Integer, specify a new mask

Return result:

  • Integer , return the previous mask

Change the current umask, Windows does not support this method

1
static Integer process.umask(String mask);

Call parameters:

  • mask : String, specify a new mask, string type octal (eg: "0664")

Return result:

  • Integer , return the previous mask

Returns the current umask, Windows does not support this method

1
static Integer process.umask();

Return result:

  • Integer , returns the current mask value

hrtime

Returns the high-precision time of the system, this time has nothing to do with the current time, only used for high-precision timing

1
static Array process.hrtime(Array diff = []);

Call parameters:

  • diff : Array, initial time for comparison

Return result:

  • Array , returns the timing time, the format is [seconds, nanoseconds]

exit

Exit the current process, and return exitCode as the process result

1
static process.exit();

Exit the current process and return the result

1
static process.exit(Integer code);

Call parameters:

  • code : Integer, return process result

cwd

Returns the current working path of the operating system

1
static String process.cwd();

Return result:

  • String , returns the current system path

chdir

Modify the current working path of the operating system

1
static process.chdir(String directory);

Call parameters:

  • directory : String, specify the new path set

uptime

Query the running time of the running environment, in seconds

1
static Number process.uptime();

Return result:

  • Number , returns a numeric value representing the time

memoryUsage

Query current process memory usage report

1
static Object process.memoryUsage();

Return result:

  • Object , return contains memory report

The memory report generates results similar to the following:

1 2 3 4 5
{ "rss": 8622080, "heapTotal": 4083456, "heapUsed": 1621800 }

among them:

  • rss returns the size of physical memory currently occupied by the process
  • heapTotal returns the v8 engine heap memory size
  • heapUsed returns the size of the heap memory being used by the v8 engine

nextTick

Start a fiber

1 2
static process.nextTick(Function func, ...args);

Call parameters:

  • func : Function, specifies the function executed by the fiber
  • args : ..., a sequence of variable parameters, this sequence will be passed to the function in the fiber

binding

Get the internal module of the specified name

1
static Value process.binding(String name);

Call parameters:

  • name : String, specify the name of the internal module to be queried

Return result:

  • Value , returns the specified internal module

open

Run the specified command line, take over the process input and output streams, and return the process object

1 2 3
static SubProcess process.open(String command, Array args, Object opts = {});

Call parameters:

  • command : String, specify the command line to run
  • args : Array, specify the list of parameters to run
  • opts : Object, specify the options to run

Return result:

  • SubProcess , returns the process object containing the running result

The options supported by opts are as follows:

1 2 3 4
{ "timeout": 100, // 单位为 ms "env": {} // 进程环境变量 }

Run the specified command line, take over the process input and output streams, and return the process object

1 2
static SubProcess process.open(String command, Object opts = {});

Call parameters:

  • command : String, specify the command line to run
  • opts : Object, specify the options to run

Return result:

  • SubProcess , returns the process object containing the running result

The options supported by opts are as follows:

1 2 3 4
{ "timeout": 100, // 单位为 ms "env": {} // 进程环境变量 }

start

Run the specified command line and return the process object

1 2 3
static SubProcess process.start(String command, Array args, Object opts = {});

Call parameters:

  • command : String, specify the command line to run
  • args : Array, specify the list of parameters to run
  • opts : Object, specify the options to run

Return result:

  • SubProcess , returns the process object containing the running result

The options supported by opts are as follows:

1 2 3 4
{ "timeout": 100, // 单位为 ms "env": {} // 进程环境变量 }

Run the specified command line and return the process object

1 2
static SubProcess process.start(String command, Object opts = {});

Call parameters:

  • command : String, specify the command line to run
  • opts : Object, specify the options to run

Return result:

  • SubProcess , returns the process object containing the running result

The options supported by opts are as follows:

1 2 3 4
{ "timeout": 100, // 单位为 ms "env": {} // 进程环境变量 }

run

Run the specified command line and return the end code of the process

1 2 3
static Integer process.run(String command, Array args, Object opts = {}) async;

Call parameters:

  • command : String, specify the command line to run
  • args : Array, specify the list of parameters to run
  • opts : Object, specify the options to run

Return result:

  • Integer , returns the result of the command

The options supported by opts are as follows:

1 2 3 4
{ "timeout": 100, // 单位为 ms "env": {} // 进程环境变量 }

Run the specified command line and return the end code of the process

1 2
static Integer process.run(String command, Object opts = {}) async;

Call parameters:

  • command : String, specify the command line to run
  • opts : Object, specify the options to run

Return result:

  • Integer , returns the result of the command

The options supported by opts are as follows:

1 2 3 4
{ "timeout": 100, // 单位为 ms "env": {} // 进程环境变量 }

Static properties

argv

Array, returns the command line parameters of the current process

1
static readonly Array process.argv;

execArgv

Array, returns the special command line parameters of the current process, these parameters are used by fibjs to set the operating environment

1
static readonly Array process.execArgv;

version

String, return fibjs version string

1
static readonly String process.version;

versions

Object, return version information of fibjs and components

1
static readonly Object process.versions;

execPath

String, query the full path of the current execution file

1
static readonly String process.execPath;

env

Object, query the environment variables of the current process

1
static readonly Object process.env;

arch

String, query the current cpu environment, possible results are'amd64','arm','arm64','ia32'

1
static readonly String process.arch;

platform

String, query the name of the current platform, possible results are'darwin','freebsd','linux', or'win32'

1
static readonly String process.platform;

pid

Integer, read the id of the process pointed to by the current object

1
static readonly Integer process.pid;

ppid

Integer, read the id of the parent process pointed to by the current object

1
static readonly Integer process.ppid;

stdin

File , query the standard input object of the current process

1
static readonly File process.stdin;

stdout

File , query the standard output object of the current process

1
static readonly File process.stdout;

stderr

File , query the standard error output object of the current process

1
static readonly File process.stderr;

exitCode

Integer, query and set the exit code of the current process

1
static Integer process.exitCode;