0x1ad2
0x1ad2

Reputation: 8062

How can I gain root access and use disk usage (du) from electron's main process?

I'm building a macOS app using Electron

I try to run the following command from the main process using ipcMain and NodeJS's 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.

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);
    }
);

Upvotes: 0

Views: 232

Answers (1)

Yaroslav Gaponov
Yaroslav Gaponov

Reputation: 2099

The problem in cd. Module sudo-prompt redirect stderror to file. Just try to run like this example or add command for return back like cd ~/Library/Caches && du -sh * && cd ${process.cwd()}

child_process.exec('push /etc\ndu -sh *\npopd', (error, stdout, stderr)=> console.log(stdout))

`

Upvotes: 1

Related Questions