Search code examples
shellxulxulrunner

What's the best way to get the output of an script (shell-script) in Xulrunner?


I'm running a script with nsIProcess.run(), and the only way I found to get the output is to write the output to a file, and then read the file from javascript.

But for some reason, when I execute it from the xulrunner application, it does not generate the file with the output. Here's my function:

function runProcess() {
    // create an nsILocalFile for the executable
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces["nsILocalFile"]);
    file.initWithPath("/home/me/my-script.sh");

    write("FILE EXISTS = " + file.exists()); // it is printing TRUE, good!


    // create an nsIProcess
    var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
    process.init(file);

    // Run the process.
    // If first param is true, calling thread will be blocked until
    // called process terminates.
    // Second and third params are used to pass command-line arguments
    // to the process.
    process.run(true, [], 0);
}

my-script.sh:

echo ok > /tmp/result.txt

Is there a better (and working) approach to get this "ok" output from my-script.sh?

--update

I'm on Ubuntu 10.04 with Xulrunner 1.9.2.15


Solution

  • There is a third-party "ipc" library used e.g. by Enigmail that allows you to capture the output of a command. It might even become part of XULrunner at some point.

    EDIT: As discussed in the comments, nsIProcess.run uses exec rather than system so the script needs to have a #! line so that the kernel can spawn the shell.