2017-08-02 69 views
0

我運行下面的代碼:STDOUT抑制方法與節點child_process

var exec = require('child_process').exec; 
var command = "security set-key-partition-list -S apple-tool:,apple: -s -k password /path/to/keychain/login.keychain-db"; 
exec(serverConfig.securityCall, function (error, stdout, stderr) { 
    if (error !== null) { 
     console.log('exec error: ' + error); 
     console.log('STDERR: ' + stderr); 
     console.log('STDOUT: ' + stdout); 
    } 
}); 

我得到的錯誤:exec error: Error: stdout maxBuffer exceeded

有沒有辦法來壓制stdout?我不需要它。 我看到這個帖子:Stdout buffer issue using node child_process

所以,我把它改成一個spawn

var spawn = require('child_process').spawn; 
var child = spawn('security', ['set-key-partition-list', '-S apple-tool:,apple: -s -k password /path/to/keychain/login.keychain-db'], {stdio:['ignore', 'ignore', 'pipe']}); 

child.stderr.on('data', function (data) { 
    console.log('stderr: ' + data); 
    stderr = 'stderr: ' + data 
}); 

child.on('close', function (code) { 
    console.log('child process exited with code ' + code); 
    if (!code) { //0 = success 1= error 
     console.log("SUCCESS"); 
    } else { 
     console.log('STDERR: ' + stderr); 
    } 
}); 

,但我得到這個錯誤:如果我跑這從它的工作原理命令行

stderr: password to unlock default: security: SecKeychainItemSetAccessWithPassword: The user name or passphrase you entered is not correct. 

,所以我知道我的密碼是正確的。 (出於安全目的,密碼和鑰匙串路徑已被編輯)。

我怎樣才能得到這個spawnexec

回答

1

您收到的錯誤來自您的security應用程序,而不是來自節點。有一個使用spawn棘手的部分。每個選項都應該是一個單獨的數組元素。

所以這個數組元素應該分開到多個元素

'-S apple-tool:,apple: -s -k password /path/to/keychain/login.keychain-db' 

喜歡的東西

['-S', 'apple-tool:,apple:', '-s', '-k', 'password', '/path/to/keychain/login.keychain-db'] 

老實說,我不明白爲什麼它沒有在文檔中很好地解釋。

+0

這工作,非常感謝。 –