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?
asked Apr 26, 2016 at 19:00
user4324324
5593 gold badges8 silver badges27 bronze badges
2 Answers 2
Try this
// app.js
require('child_process').spawn('sh', ['test.sh'], {stdio: 'inherit'});
answered Jul 5, 2016 at 10:08
oakymax
1,4741 gold badge14 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Murali N
sweet and simple
The code works for me perfectly, without any changes. When you say you are not able to see any response, does the process hang? what is the final outcome? I doubt the sudo command in the script is causing some issues. Can you eliminate that and change? I don't have sudo access, so it failed on prompt.
bash-4.1$ node app.js
[sudo] password for gireesh:
[sudo] password for gireesh:
[sudo] password for gireesh:
[Error: Error: Command failed: /bin/sh -c sh test.sh
--2016年04月27日 08:28:01-- http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-x64.tar.gz
Resolving nodejs.org... 104.20.22.46, 104.20.23.46, 2400:cb00:2048:1::6814:172e, ...
Connecting to nodejs.org|104.20.22.46|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5314716 (5.1M) [application/octet-stream]
Saving to: ânode-v0.10.26-linux-x64.tar.gz.2â
Some diagnostic steps:
- Print the child object, after the excec call. This will print the process information pertinent to the child. This will make sure the child is started.
- Install a child terminator callback, and print a debug information in it. This will make sure the child is ended.
- If still problem persists, install child stream hooks. This will make sure the instantaneous prints in the streams are output immediately, rather than at the end.
Modified 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);
}
});
console.log(child);
child.on('close', function(code) {
console.log('child ended with: ' + code);
});
child.on('error', function(err) {
console.log('child errd with: ' + err);
});
child.stdout.on('data', function(d) {
console.log('child stdout: ' + d);
});
}
exports.result = result;
Hope this helps.
answered Apr 27, 2016 at 12:52
Gireesh Punathil
1,3748 silver badges19 bronze badges
Comments
default
sh test.shto just put its path relative to the working directory. Also check your executing permissions.