如何检查 NodeJS 是否由 root 运行
使用此函数确定当前 NodeJS 进程是否由 root 用户运行:
is_current_user_root.js
function isCurrentUserRoot() {
return process.getuid() == 0; // UID 0 始终是 root
}用法示例:
is_current_user_root_usage.js
if(isCurrentUserRoot()) {
// TODO 你的 root 用户代码放在这里
} else {
// TODO 你的非 root 用户代码放在这里!
}这有效因为 root 用户的 UID 在 Linux/Unix 系统上始终为 0。
注意:你不需要
process_require.js
const process = require("process");因为 process 对象在任何 NodeJS 环境中自动导入
Check out similar posts by category:
Javascript, NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow