【问题标题】:node.js child_process.spawn output to console odd formatnode.js child_process.spawn 输出到控制台奇数格式
【发布时间】:2015-02-27 11:57:12
【问题描述】:

我正在尝试编写一个小包装器来通过 nodejs 执行 shell 命令并将命令的输出打印到控制台。到目前为止,我已经完成了很多工作,但是控制台中的输出格式......很尴尬,而且我不知道为什么。 :(

#!/usr/bin/env node

var fs = require('fs');
var spawn = require('child_process').spawn;

MyWrapper( "ls", ["-l"], function(text) {

        console.log(text)
        fs.appendFileSync('testlog.txt', text)
});

function MyWrapper(cmd, args, callBack) {

        var child = spawn(cmd, args)
        var response = ""

        child.stdout.on('data', function (buffer) { response += buffer.toString() });
        child.stdout.on('end', function() { callBack (response) });
}

testlog.txt 完全没问题:

total 5
drwxrwxr-x+ 1 user user   114 Feb 26 23:09 test.git
-rw-rw-r--+ 1 user user  7524 Feb 27 12:45 testlog.txt
-rw-rw-r--+ 1 user user   524 Feb 27 01:17 test.txt

但是控制台输出是:

total 5

drwxrwxr-x+ 1 mmay mmay  114 Feb 26 23:09 test.git

                                                  -rw-rw-r--+ 1 git  git  7750 Feb 27 12:47 testlog.txt

                                                                                                        -rw-rw-r--+ 1 git  git   524 Feb 27 01:17 test.txt

我不知道如何解决它......任何提示都会很棒:)

【问题讨论】:

  • 你的意思是行前的空格?检查缓冲液。
  • 感谢您的回复。缓冲区很好,小我很愚蠢:)

标签: node.js console console.log


【解决方案1】:

呃,犯了一个愚蠢的错误。

在登录时通过 authorized_keys 中的 command 调用脚本。还有 no-pty 标志。

【讨论】:

    猜你喜欢
    • 2013-11-19
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多