简体   繁体   English

如何从电子的主进程中获得 root 访问权限并使用磁盘使用率 (du)?

[英]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 .我尝试使用ipcMainipcMainexec从主进程运行以下命令。

// 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.

相关问题 如何从Electron的主要过程访问相机 - How to access camera from Electron's main process 如何从 Electron 中的主进程访问 BrowserWindow JavaScript 全局? - How can I access the BrowserWindow JavaScript global from the main process in Electron? 如何从渲染过程的主过程访问对象[Electron] - How do I access an object from the main process from a render process [Electron] 如何从主要过程代码中获取Electron stdout内容? - How can I get Electron stdout content from within my main process code? Electron:我可以将 http 请求从渲染器发送到 electron 中的主进程吗? - Electron: Can i send http request from renderer to main process in electron? Electron:如何从主流程(toPrint等)访问在渲染过程中渲染的网页内的Webview? - Electron : How to access webview inside a webpage which is rendered in the render process from the main process (toPrint , etc)? 电子:渲染器访问主进程? - Electron: renderer access to main process? 我们如何将消息从主进程发送到 Electron 中的渲染器进程 - How can we send messages from the main process to renderer process in Electron 您如何通过Google Chrome扩展程序访问网页的事件监听器 - How can you gain access to a web page's event listeners from a Google Chrome extension Electron:如何捕获来自主进程的所有请求响应? - Electron: How to catch all the requests response from the main process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM