如何在linux中的文件中捕获top命令的输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11729720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capture the output of a top command in a file in linux?
提问by Pradep
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”命令的输出写入文件。我做了一些谷歌搜索,发现可以通过使用以下命令来完成。
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.
其中 -n 是指定迭代次数,-b 是批处理模式。如果让 top 进行 10 次迭代,这将非常有效。但是如果我用 Ctrl-C 中断命令的运行,输出文件似乎是空的。
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?
我不会事先知道迭代次数,所以我需要手动打破它。如何在不指定迭代的情况下在文件中捕获 top 的输出?
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?
编辑:为了给这个问题提供更多的上下文,我有一个 Java 代码,它调用一个带有输入文件的工具。正如该工具将一个文件作为输入并运行一段时间,然后获取下一个文件,依此类推。我有一组 100,000 个文件需要提供给该工具。所以现在我试图监控那个特定的工具(它在 Linux 中作为一个进程运行)。由于文件太大,不需要的数据,我无法捕获整个“顶部”数据。如何捕获该进程的系统统计信息并使用 top 将其写入文件?
采纳答案by Markus Mikkolainen
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.
对我来说top -b > test.txt
,top
即使我用ctrl-c
. 我建议您先转储,然后再转储grep
生成的文件。
回答by CIGuy
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
回答by Thor
How about using while
loop and -n 1
:
如何使用while
循环和-n 1
:
while sleep 3; do
top -b -n1 | grep init > top-output.txt
done
回答by user320781
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.
它会创建 myFile.txt 但当我按 Ctrl+C 时它会是空的。所以在我启动我的 top 命令之后,我开始了第二个 top 进程。当我找到第一个顶部的 PID(进行了一些试验和错误),并通过第二个顶部将其杀死时,第一个顶部按预期写入文件。
Hope that helps!
希望有帮助!
回答by jb1
Here is the 1-liner I like to use on my mac:
这是我喜欢在 Mac 上使用的 1-liner:
top -o -pid -l 1 | grep "some regexp"
Cheers.
干杯。
回答by Suraj Bhatia
From the topcommand, we can see all the processes with their PID (Process ID). To print top output for only one process, use the following command:
从top命令中,我们可以看到所有进程及其 PID(进程 ID)。要仅打印一个进程的顶部输出,请使用以下命令:
$ top –p PID
To save top command of any process to a file, use the following command:
要将任何进程的 top 命令保存到文件,请使用以下命令:
top -p $PROCESS_ID -b > top.log
where > redirects standard output to a file.
其中 > 将标准输出重定向到文件。
回答by Balaji Boggaram Ramanarayan
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.
如果您希望在后台运行 top 命令(只是不用担心注销/睡眠等) - 您可以使用 nohup 或批处理作业或 cron 或 screen。
Using nohup (stands for : No Hang Up):
使用 nohup(代表:不挂断):
Say suppose if you save the top command in a file called top-exec.shwith following content :
假设您将 top 命令保存在名为top-exec.sh的文件中,内容如下:
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.shusing nohup as follows :
您可以为您感兴趣的任何进程替换 top 命令。然后,您可以使用 nohup执行top-exec.sh,如下所示:
$> nohup top-exec.sh &
This will redirect all the output of top command to a file named "top.log".
这会将 top 命令的所有输出重定向到名为"top.log"的文件。
回答by Kruthika C S
Solved this issue. This works even if you press Ctrl+cEven I was facing the same issue when I wanted to log Cpu%. Execute this shell script:
解决了这个问题。即使您按 Ctrl+c 这也有效即使我想记录 Cpu% 时也面临同样的问题。执行这个shell脚本:
#!/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 operationStarts 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.
- 你可以 grep 任何你想从 top 命令中提取的东西,使用这个脚本将它存储到一个文件中。
- -b :批处理模式操作以批处理模式启动 top,这对于将输出从 top 发送到其他程序或文件很有用。在这种模式下,top 不会接受输入并运行,直到您使用 -n 命令行选项设置的迭代限制或直到被终止。
- -n number,此选项指定 top 在结束前应产生的最大迭代次数或帧数。我在这里使用了-n 1。
- 做
man top
更多详情 - tee -a 使输出在控制台上可见,并将输出存储到文件中。-a 选项将输出附加到文件中。
- 在这里,我给出了 1 秒的间隔。您可以提及任何其他间隔。
Source for explanations of -b and -n: manpages
-b 和 -n 的解释来源:联机帮助页
man top
Kruthika
克鲁蒂卡
回答by Jitesh Sojitra
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:
由于控制停留在 CLI 中,CTRL+C 不是理想的解决方案。您可以使用以下命令将顶部输出转储到文件:
top -n 1 -b > top-output.txt
回答by Campa
As pointed out by @Thor in a comment, you just need to ensure that grep
is not buffering arbitrarily but per-linewith the --line-buffered
option:
正如@Thor 在评论中指出的那样,您只需要确保grep
不是任意缓冲而是每行都使用--line-buffered
选项:
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.
如果没有 grep-ing,将输出重定向top
到文件就可以了,包括中断。