• 0

How to run unix commands from nodejs?


Nodejs allows us to execute linux commands from its child process module as shown below.
var exec = require('child_process').exec;

// any unix based command
var cmdToLaunch = "ls -la";

function execCB (error, stdout, stderr) {
    if (error) {
        console.error(`exec error: ${error}`);
        return;
    }
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
}

var app = exec(cmdToLaunch, execCB);

// Use app.pid to get reference to the application and take further actions on the application

Check out the reverse of this, like how to run node script from bash script.