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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With