node.js -- 同步执行命令并获取结果
我试图在 node.js 中同步执行 child_process (是的,我知道这很糟糕,我有充分的理由)并检索 stdout 上的任何输出,但我不太清楚如何...
我发现了这个SO 帖子: node.js 同步执行系统命令 描述了如何使用图书馆(node-ffi) 来执行命令,这很好用,但我唯一能得到的是进程退出代码。命令执行的任何数据都会直接发送到标准输出 - 我如何捕获它?
> run('whoami')
username
0
换句话说,username
被 echo 到 stdout,run
的结果是 0
。
我非常宁愿弄清楚如何读取标准输出
I'm trying to execute a child_process synchronously in node.js (Yes, I know this is bad, I have a good reason) and retrieve any output on stdout, but I can't quite figure out how...
I found this SO post: node.js execute system command synchronously that describes how to use a library (node-ffi) to execute the command, and this works great, but the only thing I'm able to get is the process exit code. Any data the command executes is sent directly to stdout -- how do I capture this?
> run('whoami')
username
0
in otherwords, username
is echo'd to stdout, the result of run
is 0
.
I'd much rather figure out how to read stdout
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所以我有一个有效的解决方案,但并不完全喜欢它...只是在这里发布以供参考:
我正在使用其他 SO 帖子中引用的
node-ffi
库。我有一个函数:>>> run-sync-output
run-sync-output
并存储结果存在一个明显的问题,如果用户没有写入权限当前目录,它将失败。另外,这只是浪费精力。 :-/
So I have a solution working, but don't exactly like it... Just posting here for reference:
I'm using the
node-ffi
library referenced in the other SO post. I have a function that:>> run-sync-output
run-sync-output
synchronously and stores the resultThere's an obvious issue where if the user doesn't have write access to the current directory, it will fail. Plus, it's just wasted effort. :-/
我构建了一个 node.js 模块来解决这个问题。看看吧:)
exec-plan
更新
上述模块解决了您原来的问题,因为它允许子进程的同步链接。链中的每个链接都从链中的前一个进程获取标准输出。
I have built a node.js module that solves this exact problem. Check it out :)
exec-plan
Update
The above module solves your original problem, because it allows for the synchronous chaining of child processes. Each link in the chain gets the stdout from the previous process in the chain.
我遇到了类似的问题,最终我为此编写了一个节点扩展。您可以查看 git 存储库。它是开源的、免费的,而且都是好东西!
https://github.com/aponxi/npm-execxi
使用说明位于自述文件中。请随意提出拉取请求或提交问题!
然而它还没有返回标准输出......好吧,我今天刚刚发布了它。也许我们可以在此基础上继续发展。
无论如何,我认为值得一提。我也将此发布到类似的问题: node.js 同步执行系统命令
I had a similar problem and I ended up writing a node extension for this. You can check out the git repository. It's open source and free and all that good stuff !
https://github.com/aponxi/npm-execxi
Usage instructions are in the ReadMe file. Feel free to make pull requests or submit issues!
However it doesn't return the stdout yet... Well, I just released it today. Maybe we can build on it.
Anyway, I thought it was worth to mention it. I also posted this to a similar question: node.js execute system command synchronously
从 Node 版本 v0.11.12 开始,有一个
child_process.execSync
函数用于此。Since Node version v0.11.12, there is a
child_process.execSync
function for this.除了编写稍微不同的代码之外,实际上没有理由做任何同步的事情。
你不喜欢这个什么? (文档)
Other than writing code a little diferent, there's actually no reason to do anything synched.
What don't you like about this? (docs)