Node.js child process changes directory and starts the process

I am trying to run an external application in node.js with a 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 a callback

run.sh starts the server when I execute it using cp.exec. I expect it to start asynchronously, so my application does not wait for the server to finish. In the callback, I want to work with the server.

Please help me solve this problem.

+6
source share
2 answers

cp.exec get the working directory in the parameters 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){ }); 

to run the script in the path directory.

+11
source

The quotes are interpreted by the shell; you cannot see them if you just look at the ps output.

0
source

Source: https://habr.com/ru/post/975190/


All Articles