如何运行 ''top'' 命令 1 次并退出?

Moh*_*ani 19 command-line top

是否有任何开关可以告诉top命令以便一次扫描系统而不是一直监视它?

cha*_*aos 28

-n开关是你搜索的内容:

user@host:~$ top -n 1
Run Code Online (Sandbox Code Playgroud)

请参阅联机帮助页

   -n : Number of iterations limit as:  -n number
        Specifies the maximum number of iterations, or frames, top should produce before ending.
Run Code Online (Sandbox Code Playgroud)

如果您想停止top命令,只需按q

注意:如果您倾向于top在脚本中使用,我需要说top不是用于脚本,ps而是使用。

  • 我在脚本中使用 `top`,因为我认为 `ps` 无法读取 CPU 平均负载。我错了吗? (2认同)

bol*_*y38 9

请注意,如果您想为脚本保留“top”命令,您可以使用-b选项(“-b”选项表示“批处理模式操作”),此选项允许您将输出重定向到文件中。

例子:

  • 顶级一拍批量模式: top -bn 1 2>&1 1> /tmp/topN
  • 顶部按内存使用情况排序一拍批处理模式: top -ban 1 2>&1 1> /tmp/topAN