grep from super slow continuous streamed log and exit once string is found without buffer grep from super slow continuous streamed log and exit once string is found without buffer kubernetes kubernetes

grep from super slow continuous streamed log and exit once string is found without buffer


I suspect it's because kubectl hasn't exited that the shell doesn't continue on. If you look at the ps output, you'll notice that grep -m1 ... does actually exit, and doesn't exist anymore, but the rest of the pipe still exists.

So I suspect you'll need to invert this. In perl, for example, I would use open to open a pipe to kubectl, read the output until I found what I wanted, kill the child, and exit. In C, the same thing with popen. I'm not sure if bash gives quite that level of control.

For example:

 perl -E 'my $pid = open my $fh, "-|", qw(perl -E), q($|++; say for 1..10; say "BOOM"; say "Sleep Infinity"; sleep 50) or die "Cannot run: $!"; while(<$fh>) { if (/BOOM/) { say; kill "INT", $pid; exit 0 } }'

You'll have to replace the stuff in the open after "-|" with your own command, and the if (/BOOM/) with your own regex, but otherwise it should work.