I want to read and parse (with awk) my nginx logs in realtime. So I wrote this shell command to do this, it works if I run it in shell:
tail -f -n +1 /var/log/nginx/access.log | awk '{print $1, $7, $9}'
But I can't run it in node. I wrote this code to run my command (this is taken from the example in node.js docs), but it displays nothing:
const tail = childProcess.spawn('tail',['-f', '-n', '+1', nginxAccessLog]);
const awk = childProcess.spawn('awk', ['{print $1, $7, $9}'], { stdio: [tail.stdout, 'pipe', 'pipe'] })
tail.stdout.on('data', (data) => {
console.log('tail output', data.toString());
})
awk.stdout.on('data', (data) => {
console.log('awk output', data.toString());
})
tail.on('error', () => 'tail error')
awk.on('error', () => 'awk error')
tail.on('close', () => console.log('tail end', result));
awk.on('close', () => console.log('awk end', result));
I can see that my process are spawned (they are in the htop output), but I have no output. How can I fix this?
via serge1peshcoff
No comments:
Post a Comment