2011-03-04 62 views
0

我正在运行一个脚本nsIProcess.run(),我发现获得输出的唯一方法是将输出写入文件,然后从JavaScript中读取文件。在Xulrunner中获取脚本输出(shell脚本)的最佳方式是什么?

但由于某些原因,当我从xulrunner应用程序执行它时,它不会生成带有输出的文件。这里是我的功能:

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 

有没有更好的(和工作)的方式来得到my-script.sh这个 “OK” 输出?

--update

我在Ubuntu 10.04使用的XULrunner 1.9.2.15

+0

尝试在您的shell脚本中使用绝对路径。它的当前目录可能不是你想象的那样。 – 2011-03-04 16:40:02

+0

那么脚本是否被执行,但是文件没有被写入,或者脚本没有被执行?可以确认它确实以某种方式运行? – pc1oad1etter 2011-03-04 17:57:29

+0

@丹尼斯威廉姆森好点。不幸的是,它仍然没有创建文件。 – 2011-03-04 18:01:03

回答

0

这里有一个第三方 “IPC” 库例如使用通过Enigmail,您可以捕获命令的输出。它甚至有可能成为XULrunner的一部分。

编辑:正如评论中所述,nsIProcess.run使用exec而不是系统,因此脚本需要#!以便内核可以产生shell。

+0

是的,我开始认为这是唯一的方法.. – 2011-03-10 13:46:22

+0

但是,真的看起来像脚本甚至没有运行..任何想法为什么? – 2011-03-10 15:13:39

+0

你有没有尝试在你的脚本中放一个睡眠,让自己有机会看它运行? – Neil 2011-03-11 00:34:35

0

从上面的讨论中,听起来好像这个过程可能不会被启动。

我将要做两件事情

  • 尝试调用其他进程/可执行
  • 变空为[],也许它被悄悄地失败的,希望这是一个数组(即使是空的array)
+0

我已经为'']更改了'null',但没有更改。我不知道是否通过调用'process.init(“firefox”);'它应该启动Firefox,但也没有发生。通过调用其他'.sh'文件也不起作用。 – 2011-03-11 12:43:01

相关问题