Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing shell script from code

I am trying to execute a shell script from nodejs code

//test.sh
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-x64.tar.gz
tar -zxvf node-v0.10.26-linux-x64.tar.gz
mv node-v0.10.26-linux-x64 node-v0.10.26
sudo cp -r node-v0.10.26 /usr/local/src

//app.js
var exec = require('child_process').exec;
 var execProcess = require("./exec_process.js");
execProcess.result("sh test.sh", function(err, response){
    if(!err){
        console.log(response);
    }else {
        console.log(err);
    }
});

//exec_process.js
 var exec = require('child_process').exec;

var result = function(command, cb){
    var child = exec(command, function(err, stdout, stderr){
        if(err != null){
            return cb(new Error(err), null);
        }else if(typeof(stderr) != "string"){
            return cb(new Error(stderr), null);
        }else{
            return cb(null, stdout);
        }
    });
}

exports.result = result;

After executing app.js, I am not able to see anything in response. What I am doing wrong here?

like image 623
user4324324 Avatar asked Dec 11 '22 17:12

user4324324


1 Answers

Try this

// app.js
require('child_process').spawn('sh', ['test.sh'], {stdio: 'inherit'});
like image 72
oakymax Avatar answered Dec 28 '22 10:12

oakymax