Hello Mark,
Mark Reynolds [2021-08-25 12:38 -0400]:
This code used to work for a long time, but now it stopped working
as
expected:
let cmd = [
"/bin/sh",
"-c",
I can't possibly believe that ' vs. " has any actual influence about it --
this
makes absolutely no difference in JavaScript, as per spec [1]. This must be a
red herring.
'/usr/bin/echo TEST >> ' + setup_file
Is it possible that setup_file contains quotes?
cockpit.spawn (cmd, { superuser: true, err: "message"
})
Are you actually waiting for this promise to settle? I.e. did you remove the
.then/.catch only to shorten your email, or are they actually missing in your
code?
In a running Cockpit session, I opened the developer console and ran
cockpit.spawn(['/bin/sh', '-c', '/usr/bin/echo TEST >>
/tmp/my_file'], { superuser: true, err: "message"}).then(() =>
console.log("success")).catch(e => console.error(e))
This works, logs "succcess", and I have a root-owned /tmp/my_file with
"TEST"
in it. You can try the same -- that's much more convenient than having to
re-build a script and reloading the page.
If your promise handlers are missing, can you try to add them and see what they
complain about?
Now the output file (setup_file) is empty.
Does it get created, or is a leftover from a previous run? I.e. try to remove
it first, to see if the shell at least gets that far, or if it already aborts
ealier on. It may be a permission error (although unlikely as it runs as root,
but there's always SELinux and such -- the rejection message will tell you).
Another possible gotcha might be that you did not elevate to sudo privileges in
your cockpit session, so the spawn call would fail (it has `superuser: true`).
Again, the rejection message will tell you.
Martin
[1]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_...