如何在Linux的文件中捕获top命令的输出? [英] How to capture the output of a top command in a file in linux?

查看:127
本文介绍了如何在Linux的文件中捕获top命令的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将特定"top"命令的输出写入文件.我做了一些谷歌搜索,发现可以通过使用以下命令来完成.

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

其中-n用于指定迭代次数,而-b用于批处理模式.如果让top进行10次迭代,则效果很好.但是,如果我用Ctrl-C中断了命令的运行,则输出文件似乎为空.

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.

我不会事先知道迭代次数,所以我需要手动中断它.如何在不指定迭代的情况下捕获文件top的输出?

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.

为了提供更多有关此问题的信息,我有一个Java代码,该代码调用带有输入文件的工具.就像在工具中一样,将文件作为输入并运行一段时间,然后再获取下一个文件,依此类推.我有一组100,000个文件,需要将其送入该工具.因此,现在我试图监视该特定工具(它在Linux中作为一个进程运行).我无法捕获整个顶部"数据作为文件,因为不需要的数据可能太大了.如何捕获该进程的系统统计信息并使用top将其写入文件?

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?

推荐答案

即使我用ctrl-c破坏了它,top -b > test.txt也会存储所有来自top的输出.建议您先转储,然后转储grep结果文件.

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.

这篇关于如何在Linux的文件中捕获top命令的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆