Get stream from executing `tail -f app.log | grep error`?

49 views
Skip to first unread message

Alexey Petrushin

unread,
Jun 10, 2014, 5:48:44 AM6/10/14
to nod...@googlegroups.com
Hi, I'd like to provide administrative app with a page when you can see live logs. To do so I need to execute `tail -f app.log | grep error` and get the output chunk by chunk (streaming) as it get added to log file. But the problem is:

1. child_process.exec doesn't have streaming, it return only when the child process is terminated
2. child_process.spawn does have streaming, but it can execute only commands, it can't execute complex expressions like `tail -f app.log | grep error`

Is there an easy way to solve it?

Aria Stewart

unread,
Jun 10, 2014, 10:54:26 AM6/10/14
to nod...@googlegroups.com
Why not tail yourself — fs.createReadStream, and pipe it through a filter in javascript?

signature.asc

greelgorke

unread,
Jun 10, 2014, 11:08:47 AM6/10/14
to nod...@googlegroups.com
`tail -f app.log | grep error`

there is a .pipe() hidden here. Oh wait, here it is:

var spawn = require('child_process').spawn,
    tail  = spawn('tail', ['-f','app.log']),
    grep  = spawn('grep', ['error']);

tail.stdout.pipe(grep.stdin)
grep.stdout.pipe(whateverStream)

of course its all untested, but just to give a hint
Reply all
Reply to author
Forward
0 new messages