bash node.js 子进程更改目录并运行该进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25808024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:20:52  来源:igfitidea点击:

node.js child process change a directory and run the process

node.jsbashprocessexternal

提问by com

I try to run external application in node.js with child process like the following

我尝试使用如下子进程在 node.js 中运行外部应用程序

var cp = require("child_process");
cp.exec("cd "+path+" && ./run.sh",function(error,stdout,stderr){
})

However when I try to run it stuck, without entering the callback

但是,当我尝试运行它时卡住了,而没有进入回调

run.sh starts a server, when I execute it with cp.exec I expect it run asynchronously, such that my application doesn't wait until server termination. In callback I want to work with server.

run.sh 启动一个服务器,当我用 cp.exec 执行它时,我希望它异步运行,这样我的应用程序就不会等到服务器终止。在回调中,我想与服务器一起工作。

Please help me to solve this.

请帮我解决这个问题。

回答by Boris Kirov

cp.exec get the working directory in parameter options http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback

cp.exec 获取参数选项中的工作目录 http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback

Use

var cp = require("child_process");

cp.exec("./run.sh", {cwd: path}, function(error,stdout,stderr){
});

for running script in the "path" directory.

用于在“路径”目录中运行脚本。

回答by Michael Jaros

The quotes are interpreted by the shell, you cannot see them if you just look at psoutput.

引号由 shell 解释,如果仅查看ps输出,则无法看到它们。