简体   繁体   English

tail 命令不适用于 nodejs exec 命令

[英]tail command not working with nodejs exec command

I'm writing a quick test script that uses the tail command to monitor output that is added to a log file.我正在编写一个快速测试脚本,该脚本使用 tail 命令来监视添加到日志文件中的 output。 When I execute the tail command directly in the console (I'm using OpenWRT), it works fine as expected:当我直接在控制台中执行 tail 命令时(我使用的是 OpenWRT),它按预期工作正常:

tail -F /var/log/2022-04-26-system_server.log

Here's the program I made to test this out:这是我为测试这个而制作的程序:

const exec = require('child_process').exec;
exec("tail -F /var/log/2022-04-26-system_server.log", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

But when I run the script, it just sits there and nothing happens when new text is appended to the file.但是当我运行该脚本时,它只是坐在那里,当新文本附加到文件时没有任何反应。 What am I missing??我错过了什么??

Found the solution.找到了解决方案。 I needed to add an event to listen for incoming data on stdout (not sure why).我需要添加一个事件来监听 stdout 上的传入数据(不知道为什么)。 Here's the code I added that fixed the problem:这是我添加的解决问题的代码:

var logOutput = ""
theProcess.stdout.on('data', function(chunk) {
    logOutput += chunk.toString();
});

The output then goes into the string.然后 output 进入字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM