简体   繁体   中英

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

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

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.

Please help me to solve this.

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

Use

var cp = require("child_process");

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

for running script in the "path" directory.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM