Terminal redirection not working on MacOS Mojave?

If STDOUT is redirected to a non-terminal (e.g. a file) a default buffer size of 4096 Byte is used. In the Terminal STDOUT defaults to line buffering.

Executing the command top | grep idle | tee >> ... will fill the 4k buffer with ~45-50 Byte/s (depending on load and sys|usr|idle split). After a while the first buffer will be written to file. Here, it took ~85 seconds to write the first buffer to the file system.


So either be patient šŸ˜“ or if you want the file to be updated immediately (i.e. every second), modify grep buffering:

top | grep idle --line-buffered | tee >> ./utilization.txt

Result after a few seconds:

$ cat ./utilization.txt 
CPU usage: 9.5% user, 11.49% sys, 79.44% idle 
CPU usage: 5.24% user, 10.48% sys, 84.26% idle 
CPU usage: 2.86% user, 4.74% sys, 92.39% idle 
CPU usage: 2.3% user, 1.91% sys, 96.5% idle 
CPU usage: 3.79% user, 2.25% sys, 93.95% idle 
CPU usage: 2.3% user, 3.35% sys, 94.61% idle 
CPU usage: 1.91% user, 1.91% sys, 96.16% idle 
CPU usage: 2.15% user, 2.51% sys, 95.33% idle 
CPU usage: 2.50% user, 4.29% sys, 93.20% idle 
CPU usage: 1.43% user, 1.7% sys, 97.48% idle 

If Terminal output is also required use: top | grep idle --line-buffered | tee ./utilization.txt saves the output to file and the result is visible in the Terminal window!


You need to produce one sample of top`s output.

top -l1 | grep idle | tee ./utilization.txt