8

How to exec continuously? e.g. ls after cd?

I tried

exec = require('child_process').exec;
exec('cd ~/', 
  function(){
    exec('ls'),
    function(err, stdout, stderr){
      console.log(stdout); // this logs current dir but not ~/'s
    }
  }
)

exec('cd ~/').exec('ls', function(err, stdout, stderr){
  console.log(stdout);
})//this also fails because first exec returns a ChildProcess Object but not itself.
dotslashlu
  • 3,361
  • 4
  • 29
  • 56

4 Answers4

17

It is not possible to do this because exec and spawn creates a new process. But there is a way to simulate this. You can start a process with exec and execute multiple commands in the same time: In the command line if you want to execute 3 commands on the same line you would write:

cmd1 & cmd2 & cmd3

So, all 3 commands run in the same process and have access to the context modified by the previous executed commands. Let's take your example, you want to execute cd ../ and after that to execute dir and to view the previous directory list. In cmd you shoud write:

cd../ & dir

From node js you can start a process with exec and to tell it to start another node instance that will evaluate an inline script:

var exec = require('child_process').exec;
var script = "var exec = require('child_process').exec;exec('dir',function(e,d,er){console.log(d);});";
script = '"'+script+'"';//enclose the inline script with "" because it contains spaces
var cmd2 = 'node -e '+script;
var cd = exec('cd ../ &'+cmd2,function(err,stdout,strerr)
{
    console.log(stdout);//this would work
})

If you just want to change the current directory you should check the documentation about it http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Emil Condrea
  • 9,705
  • 7
  • 33
  • 52
  • That does not seem right, you should use the child_process spawn function, if you want to run multiple commands. The child_process exec is meant to only run once and then return the output in a buffer. – Kevin Simper Dec 10 '13 at 14:48
  • 1
    You can also use && operator instead of & ins `exec`. It runs commands one after another and only if the previous one exited without error. So `cd ../ && ls` should do the trick. – konrad Jan 21 '21 at 08:04
2

You can use nodejs promisify and async/await:

const { promisify } = require('util');
const exec = promisify(require('child_process').exec);

export default async function () {
  const cpu = await exec('top -bn1');
  const disk = await exec('df -h');
  const memory = await exec('free -m');

  const payload = {
    cpu,
    disk,
    memory,
  };

 return payload
}
ttfreeman
  • 5,076
  • 4
  • 26
  • 33
1

If you want to use cd first, better use process.chdir('~/'). Then single exec() will do the job.

anvarik
  • 6,417
  • 5
  • 39
  • 53
0

You can call exec with cwd param like so:

exec('ls -a', {
    cwd: '/Users/user'
}, (err, stdout) => {
    if (err) {
        console.log(err);           
    } else {
        console.log(stdout);
    }
})

But beware, cwd doesn't understand '~'. You can use process.env.HOME instead.

Tom
  • 556
  • 1
  • 7
  • 16