node执行子进程child_process shell命令的疑问
发布于 5 年前 作者 xulayen 4695 次浏览 来自 问答
let spawn = require('child_process').spawn;
//正常执行
var tail = spawn("pm2", ["logs", "pgyer-node-middle", "--raw"]);


//执行不抱错,只是不出结果。有友好提示
var tail3 = spawn("pm2", ["logs", "pgyer-node-middle", "--raw", "|", "grep", "-i", "'" + _q + "'"]);

//提示这个。。。。
[TAILING] Tailing last 15 lines for [|] process (change the value with --lines option)

2 回复

改成这样不抱错,但是grep -i没有效果

var tail3 = spawn("pm2", ["logs", "pgyer-node-middle", "--raw"], { "lines": " | grep -i '" + _q + "'" });

var grep = spawn('grep', ['-i', `${_q}`]);
var tail3 = spawn("pm2", ["logs", "pgyer-node-middle", "--raw"]);
tail3.stdout.pipe(grep.stdin);
tail3.stdout.on("data", function (data) {
        try {
            var content = data.toString("utf-8");
            io.emit('chat message find', content);
        } catch (e) {
            console.log(e);
        }
    });

回到顶部