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 06:54

CTRL+C is not a ideal solution due to control stays in CLI. You can use below command which dumps top output to a file:

top -n 1 -b > top-output.txt
查看更多
甜甜的少女心
3楼-- · 2020-02-17 06:57

Here is the 1-liner I like to use on my mac:

top -o -pid -l 1 | grep "some regexp"

Cheers.

查看更多
走好不送
4楼-- · 2020-02-17 07:02

It looks like the output is not writing to the file until all iterations are finished. You could solve this by wrapping with an external loop like this:

touch top-output.txt
while true; do
    top -b | grep init >> top-output.txt
done
查看更多
叛逆
5楼-- · 2020-02-17 07:03

How about using while loop and -n 1:

while sleep 3; do 
  top -b -n1 | grep init > top-output.txt
done
查看更多
对你真心纯属浪费
6楼-- · 2020-02-17 07:04

I had the exact same problem...

here was my line:

top -b -u myUser | grep -v Prog.sh | grep Prog > myFile.txt

It would create myFile.txt but it would be empty when I Ctrl+C'd it. So after I kicked off my top command, then I started a SECOND top process. When I found the first top's PID (took some trial and error), and I killed it thru the second top, the first top wrote to the file as expected.

Hope that helps!

查看更多
再贱就再见
7楼-- · 2020-02-17 07:04

If you wish to run the top command in background (just not to worry about logout/sleep, etc) - you can make use of nohup or batch job or cron or screen.

Using nohup (stands for : No Hang Up):

Say suppose if you save the top command in a file called top-exec.sh with following content :

 top -p <PID> -b > /tmp/top.log

You can replace the top command for whatever process you are interested in. Then, You can execute top-exec.sh using nohup as follows :

$> nohup top-exec.sh &

This will redirect all the output of top command to a file named "top.log".

查看更多
登录 后发表回答