centos 将Top数据导出到文件

tmb3ates  于 2022-11-07  发布在  其他
关注(0)|答案(2)|浏览(243)

因此,我一直在尝试运行以下命令来检查firefox进程的消耗。
top -b | grep "firefox" >> filename
现在我可以在终端中看到所需的输出。但是现在当我尝试将其导出到文件时,我无法做到。我正在运行的命令是:
top -b | grep "firefox" >> filenametop -b | grep "firefox" > filename
请帮帮忙。

xxslljrj

xxslljrj1#

您需要top-n参数。例如,

top -b -n 1 | grep "firefox" >> firefox.out

如果没有-n 1top将继续处理,永远不会到达grep .. top得手册页中:

-n  :Number-of-iterations limit as:  -n number
        Specifies  the  maximum  number of iterations, or frames, top
        should produce before ending.

使用while循环更新代码。除非使用类似cntr变量的内容,否则它将永远循环。如果要连续监视,请删除cntr代码:


# !/bin/sh

# 

not_done=1
cntr=0

# Look for process firefox every 1 seconds

while [ "$not_done" -eq 1 ]
do
  top -b -n 1 | grep "firefox" >> /tmp/firefox.out
  sleep 1
  ((cntr=cntr+1))
  # Addition logic to break out of loop after 10 iterations
  if [ "$cntr" -eq 10 ]
  then
    not_done=0
  fi
  echo "cntr=$cntr"
done
bd1hkmkf

bd1hkmkf2#

您必须在命令中添加标志-n来更改要显示的进程数。系统将提示您输入数目。
要在top实用程序中创建特定进程的快照,请执行带有PID(-p)标志的命令。
如果你想为进程1(PID)拍摄快照,我也建议你拍摄PID的三个快照:顶部-p 678 -B -n3〉监控输出

相关问题