【问题标题】:Suppressing STDOUT with node child_process使用节点 child_process 抑制 STDOUT
【发布时间】:2017-08-02 20:26:07
【问题描述】:

我正在运行以下代码:

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 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 一起使用?

【问题讨论】:

    标签: node.js child-process


    【解决方案1】:

    您收到的错误来自您的 security 应用程序,而不是来自 Node.js。使用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']
    

    老实说,我不明白为什么文档中没有很好地解释它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-09
      • 2013-12-29
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多