Gio.Subprocess use 'sh -c' to chain multiple commands

(This is using GJS for a extension of mine)

I have the command pkexec sh -c '/usr/bin/rm /etc/grub.d/42_custom_reboot && /usr/sbin/update-grub' that I am trying to execute with Gio.Subproccess. The problem is when it’s run it thinks that everything after -c is a separate command and thus causing sh to freak out about it /usr/bin/sh: 1: /usr/bin/rm /etc/grub.d/42_custom_reboot && /usr/sbin/update-grub: not found

I hand it a array of arguments I write out manualy but have also tried using Glib.shell_parse_argv with no luck either. Does this just not work with Gio.Subprocess or am I just doing something wrong when using it?

Function used to execute commands via Gio.Subprocess

function execCommand(argv, input = null, cancellable = null) {
    let flags = Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE;

    if (input !== null)
        flags |= Gio.SubprocessFlags.STDIN_PIPE;

    let proc = new Gio.Subprocess({
        argv: argv,
        flags: flags
    });
    proc.init(cancellable);
    return new Promise((resolve,reject) => {
        proc.communicate_utf8_async(input, cancellable, (proc, res) => {
            try {
                resolve([(function() {
                    if(!proc.get_if_exited())
                        throw new Error("Subprocess failed to exit in time!");
                    return proc.get_exit_status()
                })()].concat(proc.communicate_utf8_finish(res).slice(1)));
            } catch (e) {
                reject(e);
            }
        });
    });
}

and the call to said function

let [status, stdout, stderr] = await Utils.execCommand(['/usr/bin/pkexec', 'sh', '-c', '/usr/bin/rm /etc/grub.d/42_custom_reboot && /usr/sbin/update-grub'],)

PS: It would be nice to see this work as it would stop people from having to type there password 3-4 times for this

Probably you want:

await execCommand([
  'pkexec',
  'sh',
  '-c',
  '/usr/bin/rm /etc/grub.d/42_custom_reboot && /usr/sbin/update-grub'
]);

Thanks @andyholmes! I stand corrected. It does work, gnome didnt update correctly. Could you explain why having /usr/bin/pkexec vs just pkexec breaks things?

I can not, but there are some other flags for GSubprocess that may affect that behaviour.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.