简体   繁体   中英

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. When I execute the tail command directly in the console (I'm using OpenWRT), it works fine as expected:

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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