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”shell cmd。如下所示,pwd 可以工作,git status 正在尝试工作,但失败了,因为它不是在 git 目录中执行,但 cd cmd 失败,阻止了其他 cmd 的进一步成功执行。
在nodeJS shell 以及nodeJS+ExpressJS Web 服务器中尝试过。
每个命令都在单独的 shell 中执行,因此第一个命令cd
只影响随后终止的 shell 进程。如果你想跑步git
在特定目录中,只需让 Node 为您设置路径:
exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);
cwd
(当前工作目录)是其中之一有许多选项可供选择exec http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)