2016-03-21 12 views
0

Şef komutlarımı yürütmek için cmd kullanıyorum, node.js'yi kullanarak bunları nasıl çalıştırırım?node.js işlevinde "chef-apply script.rb" komutunu nasıl çalıştırılır?

PS C:\Users\xyz\chef-repo> chef-apply script.rb 

Bu benim kodudur node.js

exports.testscript=function(req,res){ 
var exec = require('child_process').exec; 
console.log("inside function"); 
var child = exec('chef-apply azurepro.rb' ,{cwd: 'C:\Users\anurag.s\chef-repo'}, 
    function(error, stdout, stderr){ 
    console.log(stdout); 
    console.log(stderr); 
    if (error !== null) { 
     console.log(error); 
    } 
     }); 

//child.stdin.end(); 
}; 

kullanarak bu komutu çalıştırmak istiyorum. Bu hatayı alıyorum ve komutum .bat dosyası.

{ [Error: spawn cmd.exe ENOENT] 
code: 'ENOENT', 
errno: 'ENOENT', 
syscall: 'spawn cmd.exe', 
path: 'cmd.exe', 
cmd: 'cmd.exe /s /c "chef-apply azurepro.rb"' } 
+0

[node.js kabuk komut yürütme] 'nin olası kopyası (http://stackoverflow.com/questions/14458508/node-js-shell-command-execution) – StephenKing

cevap

1

child_process.exec function'a bir göz atın. Yani çağrı aşağıdaki gibi olacaktır: Bu benim kodudur

const exec = require('child_process').exec; 
const child = exec('chef-apply script.rb', 
    (error, stdout, stderr) => { 
    # Your callback here 
}); 
+0

Komut dosyasının yürütülmesinden sonra çıkış yok. Senaryoları kullanıyorum ve bir VM'yi masmavi olarak önyükleme yapıyor. Hem powershell hem de bash'ta iyi çalışıyor ama burada ne cevap ne de herhangi bir hata mesajı var. – Anurag

+0

Stdout ve/veya stderr'i kendiniz yazdırmanız gerekir. – coderanger

0
var exec = require('child_process').exec;                            
exports.testscript=function(req,res){ 
//console.log("inside function"); 
var cmd = "chef-apply C:\\Users\\xyz\\chef-repo\\script.rb"; 
var child = exec(cmd + "," + 
    function(error, stdout, stderr){ 
    console.log(child.stdout); 
    console.log(child.stderr); 
    if (error !== null) { 
     console.log(error); 
    } 
     }); 

child.stdin.end(); 

}; 

. Hata veya çıktı almıyorum. Sanırım bir şey özlüyorum.

+0

Neden exec 'parametrelerini birleştiriyorsunuz? Ayrıca, cevabı kaldırmanızı ve soruya kodu eklemenizi öneririm. Konuşma akışımızı daha net hale getirirdi. – max

İlgili konular