title: How to capture the output of a top command in a file in linux?
body: <p>I want to write the output of a specific 'top' command to a file. I did some googling and find out that it can be done by using the following command. </p>
  
  <pre><code>top -n 10 -b &gt; top-output.txt
  </code></pre>
  
  <p>where -n is to specify the number of iterations and -b is for batch mode. This works very well if let top for the 10 iterations. But if i break the running of the command with a Ctrl-C, the output file seems to be empty. </p>
  
  <p>I won't be knowing the number of iterations beforehand, so i need to break it manually. How can i capture the output of top in a file without specifying iterations?</p>
  
  <p>The command which I am trying to use precisely is </p>
  
  <pre><code>top -b | grep init &gt; top-output.txt
  </code></pre>
  
  <p>and break it whenever i want. But it doesn't work. </p>
  
  <p>EDIT: To give more context to the question, I have a Java Code which invokes a tool with an Input File. As in the tool takes a file as an input and runs for some time, then takes the next file and so on. I have a set of 100,000 files which need to be fed to the tool. So now I am trying to monitor that specific tool ( It runs as a process in Linux). I cannot capture the whole of 'top' s data as the file as would be too huge with unwanted data. How to capture the system stats of just that process and write it to a file using top? </p>