Bash, nested commands and redirects

I am trying to track the CPU usage of a process with the following command:

top -b -d 1 | grep myprocess.exe 

Next, I would like to redirect this to a log file, for example

  top -b -d 1 | grep myprocess.exe > output.log 

Now this does not work, because it thinks I'm grepping myprocess.exe> ​​output.log instead of myprocess.exe

Does anyone know how I can make this redirect work?

+4
source share
2 answers

Now this actually does not work, because it believes that I grepping myprocess.exe > output.log instead of myprocess.exe

Wrong. Everything should be fine. In the first example, the pipeline runs with stdout installed on your terminal (this way you see the output, but nothing is written to the file). The second example runs the pipeline with stdout set to output.log (this way you don't see the output, but it will go directly to your file).

If you want the result to be written for both, you need another process that gets your previous stdout pipeline as stdin and duplicates it. How:

 previous_pipeline | tee output.log 

tee will print on stdout what it gets on stdin (So ​​for stdout , everything is the same as before), but additionally open another file (specified as cmdline arg) and write a copy to it.

+2
source

Try tee :

 top -b -d 1 | grep myprocess.exe | tee output.log 

If you want it to not show the output:

 top -b -d 1 | grep myprocess.exe | tee output.log > /dev/null 
+1
source

Source: https://habr.com/ru/post/1393058/


All Articles