[英]How can I gain root access and use disk usage (du) from electron's main process?
I'm building a macOS app using Electron我正在使用Electron构建一个 macOS 应用程序
I try to run the following command from the main process using ipcMain
and NodeJS's exec
.我尝试使用
ipcMain
和ipcMain
的exec
从主进程运行以下命令。
// Traverse to a directory and use disk usage to check folder sizes
cd ~/Library/Caches && du -sh *
The command gets executed the way I want it too but it throws an exception.该命令也以我想要的方式执行,但会引发异常。
Uncaught Exception:
Error: Command failed: cd ~/Library/Caches && du -sh *
du: DEDUCTED: Operation not permitted
at /Users/0x1ad2/Projects/DEDUCTED/node_modules/sudo-prompt/index.js:390:27
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61:3)
I also tried to attach the package sudo-prompt so the application can have root access.我还尝试附加包sudo-prompt以便应用程序可以具有 root 访问权限。
No luck so far.到目前为止没有运气。
Answer回答
const exec = require("child_process").exec;
exec(
`cd ~/Library/Caches && du -sh * && cd ${process.cwd()}`,
(error, stdout, stderr) => {
console.log(error);
console.log(stdout);
console.log(stderr);
}
);
The problem in cd
. cd
的问题。 Module sudo-prompt
redirect stderror to file .模块
sudo-prompt
将 stderror 重定向到 file 。 Just try to run like this example or add command for return back like cd ~/Library/Caches && du -sh * && cd ${process.cwd()}
试着像这个例子一样运行或添加返回命令
cd ~/Library/Caches && du -sh * && cd ${process.cwd()}
child_process.exec('push /etc\ndu -sh *\npopd', (error, stdout, stderr)=> console.log(stdout))
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.