gpt4 book ai didi

node.js - Node.js 中使用 spawn() 的多命令 shell 作为分 ionic 进程

转载 作者:太空宇宙 更新时间:2023-11-03 23:25:52 25 4
gpt4 key购买 nike

引用:https://nodejs.org/api/child_process.html#child_process_options_detached

大家好,

所以我需要spawn一个子进程,spawn因为exec不允许options.detachedchild.unref();,这意味着它可以与父进程解耦,允许子进程自己运行和完成,反之亦然,父进程也可以(在我们的特殊情况下,父进程可以在长时间运行的子进程之前死亡,在这种情况下更新,无需像 exec 那样等待子进程即可完成)。

我们有一个由 Node (父)应用程序构建的长连接的 ("... ; ... ; ...") 命令,但是像 spawn("echo stuff >>stderr.log") 不起作用,只有 spawn('ls', [-l]),我显然无法链接命令(因为它也在他的文档中引用,并且在 SO 上多次引用。

TLDR;

我们需要使用 spawn,但 spawn 无法处理链式 shell 命令。我现在真的需要在 bash 中编写命令并执行吗,这真的是唯一的选择吗?

谢谢

最佳答案

注意 spawnshell 选项:

If true, runs command inside of a shell. Uses '/bin/sh' on UNIX, and 'cmd.exe' on Windows. A different shell can be specified as a string. The shell should understand the -c switch on UNIX, or /d /s /c on Windows. Defaults to false (no shell).

所以:

let child = child_process.spawn('foo; bar; blah', { shell : true });

编辑:如果您使用的 Node 版本不支持此选项,这里有一个替代方案:

let child = child_process.spawn('/bin/sh', [ '-c', 'foo; bar; blah' ]);

关于node.js - Node.js 中使用 spawn() 的多命令 shell 作为分 ionic 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44431993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com