Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the output of a top command in a file in linux?

Tags:

file

linux

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.

top -n 10 -b > top-output.txt 

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.

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?

The command which I am trying to use precisely is

top -b | grep init > top-output.txt 

and break it whenever i want. But it doesn't work.

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?

like image 238
Pradep Avatar asked Jul 30 '12 21:07

Pradep


People also ask

How do I redirect top output to a file in Linux?

However, besides real time viewing of the running system, top command output can be saved to a file, by using the -b flag, which instructs top to operate in batch mode and -n flag to specify the amount of iteration the command should output.

How do you log the top command output?

Top is the realtime monitor of the running processes in a Linux system. To log the top running processes, use the following command: top -b -n 1 . -b = Batch mode operation - Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file.

How do you copy the output of a command to a file in Linux?

Method 1: Use redirection to save command output to file in Linux. You can use redirection in Linux for this purpose. With redirection operator, instead of showing the output on the screen, it goes to the provided file. The > redirects the command output to a file replacing any existing content on the file.

What is the output of top command in Linux?

The output displays the summary area (the dashboard with resource usage stats) and the task area (a list of all processes). top updates the information every three seconds by default. If the process list is long, scroll through it using the Up and Down arrows and Page Up and Page Down keys. To quit top , press q.


1 Answers

for me top -b > test.txt will store all output from top ok even if i break it with ctrl-c. I suggest you dump first, and then grep the resulting file.

like image 191
Markus Mikkolainen Avatar answered Oct 01 '22 04:10

Markus Mikkolainen