📜  Node.js process.setgid() 方法

📅  最后修改于: 2022-05-13 01:56:32.372000             🧑  作者: Mango

Node.js process.setgid() 方法

process.setgid() 方法是 process 模块的内置应用程序编程接口,用于设置 Node.js 进程的组标识。
句法:

process.setgid(id)

参数:此方法接受上面提到的单个参数,如下所述:

  • id:它是一个必需的参数,它包含一个字符串或一个整数,表示组名或 ID,如果传递了组名,它会在解析关联的数字 ID 时阻塞。

返回:不返回任何值。
注意:此函数仅适用于 POSIX 平台。在 windows 或 android 平台上不可用,因此会导致错误,即 TypeError, setgid is not a 函数。
下面的例子说明了在 Node.js 中process.setgid() 方法的使用:
示例 1:

javascript
// Node.js program to demonstrate the    
// process.setgid() Method
  
// Including process module
const process = require('process');
 
// Printing the group identity of
// the Node.js process that checks
// whether the methods exists or not
if (process.getgid && process.setgid ) {
     
  // Setting gid
  process.setgid(400);
   
  // Printing getgid() value
  console.log("The group identity of the Node.js"
      + " process:", process.getgid());
}


javascript
// Node.js program to demonstrate the    
// process.setgid() Method
  
// Including process module
const process = require('process');
 
// Set gid and check whether the
// method exists or not
if (process.setgid) {
   
  // Within try catch
  try {
    process.setgid(696);
    console.log("gid has successfully been set");
  } catch (err) {
    console.log("Failed to set gid:", err);
  }
}
 
// Check whether the method exists or not
if (process.getgid) {
   
  // Printing getgid() value
  console.log("The group identity of the Node.js"
      + " process:", process.getgid());
}


输出:

The group identity of the Node.js process: 400

示例 2:

javascript

// Node.js program to demonstrate the    
// process.setgid() Method
  
// Including process module
const process = require('process');
 
// Set gid and check whether the
// method exists or not
if (process.setgid) {
   
  // Within try catch
  try {
    process.setgid(696);
    console.log("gid has successfully been set");
  } catch (err) {
    console.log("Failed to set gid:", err);
  }
}
 
// Check whether the method exists or not
if (process.getgid) {
   
  // Printing getgid() value
  console.log("The group identity of the Node.js"
      + " process:", process.getgid());
}

输出:

gid has successfully been set
The group identity of the Node.js process: 696

注意:上面的程序将使用 node filename.js 命令编译和运行。
参考: https://nodejs.org/api/process.html#process_process_setgid_id