简体   繁体   English

Linux将顶部输出存储到文件中

[英]Linux storing top output to a file

I wanted to execute the top command as shown below and store the output to a file. 我想执行如下所示的top命令,并将输出存储到文件中。

xyz.sh xyz.sh

#!/bin/sh 
while :
do
top -n 1 
sleep 5
done

when I run the code, It executes and produces the output fine: 当我运行代码时,它将执行并产生良好的输出:

sh xyz.sh 

but when I try to execute the code in background its getting killed. 但是当我尝试在后台执行代码时,它就被杀死了。

sh xyz.sh > xyz.log &

any ideas how can I capture top output? 有什么想法可以捕获最高的输出吗?

The top cli command behaves different to what you might be used to from other utilities. top cli命令的行为与您从其他实用程序习惯的行为不同。 This is because it does not work in a typical filter like way reading from stdin and writing to stdout. 这是因为它无法在典型的过滤器中工作,例如从stdin读取并写入stdout的方式。 Instead it emulates a terminal. 相反,它模拟终端。 This means that you cannot simply redirect its output, because there is no output written to standard out. 这意味着您不能简单地重定向其输出,因为没有任何输出写入标准输出。

Check the man page (which you should always do...). 检查手册页(您应该始终这样做...)。 You will notice the -b flag which changes that behaviour. 您会注意到-b标志更改了该行为。 This will allow you to do what you try. 这将使您可以尝试做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM