node.js -- 同步执行命令并获取结果

发布于 2024-12-01 01:38:21 字数 505 浏览 1 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

毁梦 2024-12-08 01:38:21

所以我有一个有效的解决方案,但并不完全喜欢它...只是在这里发布以供参考:

我正在使用其他 SO 帖子中引用的 node-ffi 库。我有一个函数:

  • 接受给定的命令
  • 附加 >>> run-sync-output
  • 执行它
  • 同步读取 run-sync-output 并存储结果
  • 删除此 tmp 文件
  • 返回结果

存在一个明显的问题,如果用户没有写入权限当前目录,它将失败。另外,这只是浪费精力。 :-/

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:

  • takes in a given command
  • appends >> run-sync-output
  • executes it
  • reads run-sync-output synchronously and stores the result
  • deletes this tmp file
  • returns result

There'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. :-/

小伙你站住 2024-12-08 01:38:21

我构建了一个 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.

深海里的那抹蓝 2024-12-08 01:38:21

我遇到了类似的问题,最终我为此编写了一个节点扩展。您可以查看 git 存储库。它是开源的、免费的,而且都是好东西!

https://github.com/aponxi/npm-execxi

ExecXI 是一个用 C++ 编写的节点扩展,用于执行 shell 命令
将命令的输出一一输出到控制台
即时的。存在可选的链式和非链式方式;意义
您可以选择在命令失败后停止脚本
(被锁住),或者你可以像什么都没发生一样继续!

使用说明位于自述文件中。请随意提出拉取请求或提交问题!

然而它还没有返回标准输出......好吧,我今天刚刚发布了它。也许我们可以在此基础上继续发展。

无论如何,我认为值得一提。我也将此发布到类似的问题: 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

ExecXI is a node extension written in C++ to execute shell commands
one by one, outputting the command's output to the console in
real-time. Optional chained, and unchained ways are present; meaning
that you can choose to stop the script after a command fails
(chained), or you can continue as if nothing has happened !

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

层林尽染 2024-12-08 01:38:21

从 Node 版本 v0.11.12 开始,有一个 child_process.execSync 函数用于此。

Since Node version v0.11.12, there is a child_process.execSync function for this.

爱你不解释 2024-12-08 01:38:21

除了编写稍微不同的代码之外,实际上没有理由做任何同步的事情。

你不喜欢这个什么? (文档

var exec = require('child_process').exec;

exec('whoami', function (error, username) {
    console.log('stdout: %s', username);
    continueWithYourCode();
});

Other than writing code a little diferent, there's actually no reason to do anything synched.

What don't you like about this? (docs)

var exec = require('child_process').exec;

exec('whoami', function (error, username) {
    console.log('stdout: %s', username);
    continueWithYourCode();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文