Home > database >  Forward a stream from a bash script
Forward a stream from a bash script

Time:10-15

I have a helper script to debug a server in go, named debug.sh, returning JSON logs, and able to deal with eventual errors (panics, ...)

!#/bin/sh

go run server.go 2>&1 | jq -Rr '. as $raw | try fromjson catch $raw'

Then when I do ./debug.sh I have my logs printed in a nice way, and errors are catched if any.

./debug.sh
{
  "severity": "info",
  "message": "Worker 095d1c8a-5abe-47a3-b945-68be2ded4d83 ready."
}

But If want to do an extra processing :

./debug.sh | jq -r ".message"

I have nothing in output.

I guess that's because the pipe is blocked in the subscript, and I could just do all the JQ thing from my terminal, but I want the script to be easy to use.

How can I solve that ?

CodePudding user response:

Commands in pipes are by default fully buffered. Disable buffering if you want to have them executed in an interactive form.

jq --unbuffered, python -u or stdbuf -oL are some examples that allow to run commands in an unbuffered fashion.

  • Related