Filtering Process Output With tee - Gunnar Morling

submited by
Style Pass
2024-02-11 19:00:14

Recently I ran into a situation where it was necessary to capture the output of a Java process on the stdout stream, and at the same time a filtered subset of the output in a log file. The former, so that the output gets picked up by the Kubernetes logging infrastructure. The letter for further processing on our end: we were looking to detect when the JVM stops due to an OutOfMemoryError, passing on that information to some error classifier.

Simply redirecting the standard output stream of the process to a file wouldn’t satisfy the first requirement. Instead, the tee command offers a solution: it reads from stdin and writes everything to stdout as well as a file:

This kinda gives us what we want, but we lack control over the size of that log file. As is, it can grow indefinitely, eventually causing the application’s pod to run out of disk space. For the case at hand, we’re just interested in specific lines anyways. So ideally the content written to the log file would be filtered accordingly, while exposing the complete output to the Kubernetes log collector via stdout.

To accommodate that requirement, process substitution can be used. In a nutshell, it provides a bridge between the standard input and output streams and files:

Leave a Comment