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?
Try this
// app.js
require('child_process').spawn('sh', ['test.sh'], {stdio: 'inherit'});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With