如何在linux中捕获*命令的输出?

时间:2021-08-14 02:56:22

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 wont be knowing the number of iterations before hand, so i need to break it manually. How can i capture the output of top in a file without specifying iterations ?

我不会事先知道迭代的数量,所以我需要手动打破它。如何在不指定迭代的情况下捕获文件顶部的输出?

The command which iam trying to use precisely is

我正试图精确地使用的命令是。

top -b | grep init > top-output.txt

and break it whenever i want. But it doesnt 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 a 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代码,它调用一个带有输入文件的工具。在工具中,将一个文件作为输入并运行一段时间,然后获取下一个文件,以此类推。我有一套10万份文件,需要喂给工具。因此,现在我尝试监视这个特定的工具(它在linux中作为一个进程运行)。我不能将“top”的全部数据捕获为文件,因为对于不需要的数据来说,文件太大了。如何捕获该进程的系统状态并使用top将其写入文件?

10 个解决方案

#1


29  

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.

对我来说是>测试。txt将从顶部存储所有输出,即使我用ctrl-c中断它。我建议您先转储文件,然后对结果文件进行grep。

#2


6  

How about using while loop and -n 1:

如何使用while循环和- n1:

while sleep 3; do 
  top -b -n1 | grep init > top-output.txt
done

#3


4  

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

#4


1  

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

下面是我喜欢在mac电脑上使用的一行代码:

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

Cheers.

欢呼。

#5


1  

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:

解决了这个问题。即使你按下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.
  • 您可以从top命令中提取任何内容,使用这个脚本将其存储到一个文件中。
  • -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.
  • -b:批处理模式操作以批处理模式启动,这对于将输出从顶部发送到其他程序或文件是很有用的。在这种模式下,top将不接受输入并运行,直到您使用-n命令行选项设置的迭代限制或直到被杀死为止。
  • -n number, this option specifies the maximum number of iterations, or frames, top should produce before ending. Here I've used -n 1.
  • -n数字,这个选项指定最大的迭代次数,或帧,顶部应该在结束前产生。这里我用了- n1。
  • 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.
  • tee -a使输出能够在控制台中可见,并将输出存储到文件中。-选项将输出附加到文件。
  • Here, I have given an interval of 1 second. You can mention any other interval.
  • 这里,我给出了1秒的间隔。你可以提到任何其他的间隔。

Source for explanations of -b and -n: manpages

来源解释-b和-n:手册。

man top

Kruthika

Kruthika

#6


0  

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。但是如果我按Ctrl+C键,它就会是空的。因此,在我启动了top命令之后,我启动了第二个top进程。当我发现第一个顶部的PID时(经过一些尝试和错误),我通过第二个顶部将其删除,第一个顶部按预期写入文件。

Hope that helps!

希望会有帮助!

#7


0  

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或屏幕。

Using nohup (stands for : No Hang Up):

使用nohup(代表:不挂电话):

Say suppose if you save the top command in a file called top-exec.sh with 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.sh using nohup as follows :

您可以替换您感兴趣的任何进程的top命令。然后,您可以执行top-exec。sh使用nohup如下:

$> nohup top-exec.sh &

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

这将把top命令的所有输出重定向到一个名为“top.log”的文件。

#8


0  

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

#9


0  

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:

正如@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,将顶部的输出重定向到一个文件就可以了,包括中断。

#10


-1  

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:

从上面的命令中,我们可以看到所有进程的PID(进程ID)。若要只打印一个进程的top输出,请使用以下命令:

$ 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.

>将标准输出重定向到文件。

#1


29  

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.

对我来说是>测试。txt将从顶部存储所有输出,即使我用ctrl-c中断它。我建议您先转储文件,然后对结果文件进行grep。

#2


6  

How about using while loop and -n 1:

如何使用while循环和- n1:

while sleep 3; do 
  top -b -n1 | grep init > top-output.txt
done

#3


4  

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

#4


1  

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

下面是我喜欢在mac电脑上使用的一行代码:

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

Cheers.

欢呼。

#5


1  

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:

解决了这个问题。即使你按下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.
  • 您可以从top命令中提取任何内容,使用这个脚本将其存储到一个文件中。
  • -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.
  • -b:批处理模式操作以批处理模式启动,这对于将输出从顶部发送到其他程序或文件是很有用的。在这种模式下,top将不接受输入并运行,直到您使用-n命令行选项设置的迭代限制或直到被杀死为止。
  • -n number, this option specifies the maximum number of iterations, or frames, top should produce before ending. Here I've used -n 1.
  • -n数字,这个选项指定最大的迭代次数,或帧,顶部应该在结束前产生。这里我用了- n1。
  • 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.
  • tee -a使输出能够在控制台中可见,并将输出存储到文件中。-选项将输出附加到文件。
  • Here, I have given an interval of 1 second. You can mention any other interval.
  • 这里,我给出了1秒的间隔。你可以提到任何其他的间隔。

Source for explanations of -b and -n: manpages

来源解释-b和-n:手册。

man top

Kruthika

Kruthika

#6


0  

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。但是如果我按Ctrl+C键,它就会是空的。因此,在我启动了top命令之后,我启动了第二个top进程。当我发现第一个顶部的PID时(经过一些尝试和错误),我通过第二个顶部将其删除,第一个顶部按预期写入文件。

Hope that helps!

希望会有帮助!

#7


0  

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或屏幕。

Using nohup (stands for : No Hang Up):

使用nohup(代表:不挂电话):

Say suppose if you save the top command in a file called top-exec.sh with 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.sh using nohup as follows :

您可以替换您感兴趣的任何进程的top命令。然后,您可以执行top-exec。sh使用nohup如下:

$> nohup top-exec.sh &

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

这将把top命令的所有输出重定向到一个名为“top.log”的文件。

#8


0  

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

#9


0  

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:

正如@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,将顶部的输出重定向到一个文件就可以了,包括中断。

#10


-1  

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:

从上面的命令中,我们可以看到所有进程的PID(进程ID)。若要只打印一个进程的top输出,请使用以下命令:

$ 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.

>将标准输出重定向到文件。