var sys = require(\'sys\'),
exec = require(\'child_process\').exec;
exec(\"cd /home/ubuntu/distro\", function(err, stdout, stderr) {
console.log(\"cd: \" +
Your shell IS executing cd
but it's just that each shell throws away it's working directory after it's finished. Hence you're back at square one.
In your case, you don't need to call exec() more than once. You can make sure your cmd
variable contains multiple instructions instead of 1. CD will work in this case.
var cmd = `ls
cd foo
ls`
var exec = require('child_process').exec;
exec(cmd, function(err, stdout, stderr) {
console.log(stdout);
})
Note: This code should work on Linux but not Windows. See here
Each command is executed in a separate shell, so the first cd
only affects that shell process which then terminates. If you want to run git
in a particular directory, just have Node set the path for you:
exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);
cwd
(current working directory) is one of many options available for exec.
It is working. But then it is throwing the shell away. Node creates a new shell for each exec
.
Here are options that can help: http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback