1

I want to execute multiple Shell commands from a node.js application consecutively. In my case, exec(command1 & command2) does not work as it should. It works for simple commands like ls and pwd, but I need to start a server and then execute special commands on it and there, it only executes the very first command and nothing more. When I work on my command-line interface, I just type in one command after the other and I need a facility to execute them in the same manner, but automatically starting from a node.js application - and all of them in the same process!

1 Answer 1

3

You could use child_process module to achieve that.

Here's an example code to demonstrate the usage

var child_process = require('child_process');

var task1 = child_process.exec('node --version', function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});

var task2 = child_process.exec('npm --version', function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help! I had already tried child_process.exec, but in a bit different way. My problem hasn't been solved, but now I'm quite sure that its cause is on the server I want to access and not in node.js.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.