我正在尝试从 nodejs 代码执行 shell 脚本
//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;
执行 app.js 后,我看不到任何响应。我在这里做错了什么?
最佳答案
试试这个
// app.js
require('child_process').spawn('sh', ['test.sh'], {stdio: 'inherit'});
关于node.js - 从代码执行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36873566/