代码之家  ›  专栏  ›  技术社区  ›  Matt Kuhns

使用节点child\u进程抑制STDOUT

  •  1
  • Matt Kuhns  · 技术社区  · 7 年前

    我正在运行以下代码:

    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.
    

    如果我从命令行运行它,它会工作,所以我知道我的密码是正确的。(为了安全起见,已对密码和钥匙链路径进行了编辑)。

    我该怎么处理这个 exec ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   LEQADA    7 年前

    你得到的错误来自你的 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']
    

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