How to capture the output of a top command in a fi

2020-02-17 06:26发布

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?

标签: linux file
10条回答
啃猪蹄的小仙女
2楼-- · 2020-02-17 07:11

As pointed out by @Thor in a comment, you just need to ensure that grep is not buffering arbitrarily but per-line with the --line-buffered option:

top -bn 10 | grep 'init' --line-buffered | tee top-output.txt

Without grep-ing, redirecting the output of top to a file works just fine, interrupt included.

查看更多
三岁会撩人
3楼-- · 2020-02-17 07:16

From the top command, we can see all the processes with their PID (Process ID). To print top output for only one process, use the following command:

$ top –p PID

To save top command of any process to a file, use the following command:

top -p $PROCESS_ID -b  > top.log

where > redirects standard output to a file.

查看更多
时光不老,我们不散
4楼-- · 2020-02-17 07:19

Solved this issue. This works even if you press Ctrl+c Even I was facing the same issue when I wanted to log Cpu%. Execute this shell script:

#!/bin/sh
while true; do
    echo "$(top -b -n 1 | grep init)"  | tee -a top-output.log
    sleep 1
done
  • You can grep anything you wanna extract out of top command, use this script to store it to a file.
  • -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. In this mode, top will not accept input and runs until the iterations limit you've set with the -n command-line option or until killed.
  • -n number, this option specifies the maximum number of iterations, or frames, top should produce before ending. Here I've used -n 1.
  • Do man top for more details
  • tee -a enables the output to be visible on the console and also stores the output onto the file. -a option appends the output to the file.
  • Here, I have given an interval of 1 second. You can mention any other interval.

Source for explanations of -b and -n: manpages

man top

Kruthika

查看更多
Rolldiameter
5楼-- · 2020-02-17 07:20

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.

查看更多
登录 后发表回答