[英]tail command not working with nodejs exec command
我正在編寫一個快速測試腳本,該腳本使用 tail 命令來監視添加到日志文件中的 output。 當我直接在控制台中執行 tail 命令時(我使用的是 OpenWRT),它按預期工作正常:
tail -F /var/log/2022-04-26-system_server.log
這是我為測試這個而制作的程序:
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}`);
});
但是當我運行該腳本時,它只是坐在那里,當新文本附加到文件時沒有任何反應。 我錯過了什么??
找到了解決方案。 我需要添加一個事件來監聽 stdout 上的傳入數據(不知道為什么)。 這是我添加的解決問題的代碼:
var logOutput = ""
theProcess.stdout.on('data', function(chunk) {
logOutput += chunk.toString();
});
然后 output 進入字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.