var sys = require('sys'),
exec = require('child_process').exec;
exec("cd /home/ubuntu/distro", function(err, stdout, stderr) {
console.log("cd: " + err + " : " + stdout);
exec("pwd", function(err, stdout, stderr) {
console.log("pwd: " + err + " : " + stdout);
exec("git status", function(err, stdout, stderr) {
console.log("git status returned " ); console.log(err);
})
})
})
cd: null :
pwd: null : /
git status returned
{ [Error: Command failed: fatal: Not a git repository (or any of the parent directories): .git ] killed: false, code: 128, signal: null }
nodeJS exec不适用于"cd“外壳命令。如下所示,pwd工作,git状态正在尝试工作但失败,因为它不是在git目录中执行的,但cd cmd无法停止其他cmd的进一步成功执行。在nodeJS shell和nodeJS+ExpressJS well服务器中尝试过。
发布于 2013-03-26 13:25:46
每个命令都在单独的shell中执行,因此第一个cd
只影响shell进程,然后该进程终止。如果您想在特定目录中运行git
,只需让Node为您设置路径:
exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);
cwd
(当前工作目录)是many options available for exec
中的一种。
发布于 2013-03-26 13:25:57
它起作用了。但随后它就把壳扔掉了。节点为每个exec
创建一个新壳。
下面是一些有用的选项:http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
https://stackoverflow.com/questions/15629923
复制相似问题