How to use the Linux top command

本文详细介绍了Linux top命令的使用方法,包括如何显示当前系统上运行的所有进程,并通过自动更新的屏幕实时查看CPU和内存使用情况。重点讲解了如何通过按键操作对输出进行排序,以便快速定位资源占用高的进程。同时,提供了top命令的快捷键帮助,便于用户高效管理系统资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://alvinalexander.com/linux/unix-linux-top-command-cpu-memory

How to use the Linux top command

Linux processes FAQ: Is there a utility to show Linux processes interactively, like the Unix/Linux pscommand, but more like a GUI ro character-based interactive tool?

I was just writing a friend about how to use the Linux ps command, when I thought why bother, what he really needs to see right now is the top command. The Linux top command is an interactive utility that displays a character-based screen of all processes running on the current system. The screen updates itself every few seconds, and you can sort the screen contents by characteristics like CPU Usage or Memory Use.

In this tutorial we'll take a look at the most common uses of the top command, including showing how to sort the output by CPU and memory use, and change the top command display.

The Unix and Linux top command

When you issue the top command like this:

$ top

you'll see a screen that looks something like this:

The Unix and Linux top command shows memory and cpu use

As mentioned, this display will update itself automatically, depending on any settings that you have configured, and the version of the top command you're using.

Linux top command help

To get help on how to use the top command, just press the letter ? or h when the command is running. Doing so will display the following top command help screen:

Help screen for the Unix and Linux top command

In case I made that image too small, here's the content of this top help screen in text format:

Z,B       Global: 'Z' change color mappings; 'B' disable/enable bold
l,t,m     Toggle Summaries: 'l' load avg; 't' task/cpu stats; 'm' mem info
1,I       Toggle SMP view: '1' single/separate states; 'I' Irix/Solaris mode

f,o     . Fields/Columns: 'f' add or remove; 'o' change display order
F or O  . Select sort field
<,>     . Move sort field: '<' next col left; '>' next col right
R,H     . Toggle: 'R' normal/reverse sort; 'H' show threads
c,i,S   . Toggle: 'c' cmd name/line; 'i' idle tasks; 'S' cumulative time
x,y     . Toggle highlights: 'x' sort field; 'y' running tasks
z,b     . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y')
u       . Show specific user only
n or #  . Set maximum tasks displayed

k,r       Manipulate tasks: 'k' kill; 'r' renice
d or s    Set update interval
W         Write configuration file
q         Quit
        ( commands shown with '.' require a visible task display window ) 
Press 'h' or '?' for help with Windows,

Sorting the Linux top command display

In my opinion, the most important thing you need to know from this help screen is how to sort the topcommand display/output. Using the top command that comes with my CentOS Linux system as of September, 2009, you sort the output by first pressing the O key (the uppercase letter 'o'), then choosing a sort column on the following screen.

Here are two quick examples. First, to sort the top command output by memory use, follow these steps:

  1. Press 'O'
  2. Press 'N'
  3. Press [Enter]

Or, to sort the top command display by CPU usage, follow these steps:

  1. Press 'O'
  2. Press 'K'
  3. Press [Enter]

Unix/Linux top command sort options

Here's the text version of the Linux top command sort options screen:

Current Sort Field:  K  for window 1:Def
Select sort field via field letter, type any other key to return 

  a: PID        = Process Id                   z: Flags      = Task Flags <sched.h>
  b: PPID       = Parent Process Pid
  c: RUSER      = Real user name             Note1:
  d: UID        = User Id                      If a selected sort field can't be
  e: USER       = User Name                    shown due to screen width or your
  f: GROUP      = Group Name                   field order, the '<' and '>' keys
  g: TTY        = Controlling Tty              will be unavailable until a field
  h: PR         = Priority                     within viewable range is chosen.
  i: NI         = Nice value
  j: P          = Last used cpu (SMP)        Note2:
* K: %CPU       = CPU usage                    Field sorting uses internal values,
  l: TIME       = CPU Time                     not those in column display.  Thus,
  m: TIME+      = CPU Time, hundredths         the TTY & WCHAN fields will violate
  n: %MEM       = Memory usage (RES)           strict ASCII collating sequence.
  o: VIRT       = Virtual Image (kb)           (shame on you if WCHAN is chosen)
  p: SWAP       = Swapped size (kb)
  q: RES        = Resident size (kb)
  r: CODE       = Code size (kb)
  s: DATA       = Data+Stack size (kb)
  t: SHR        = Shared Mem size (kb)
  u: nFLT       = Page Fault count
  v: nDRT       = Dirty Pages count
  w: S          = Process Status
  x: COMMAND    = Command name/line
  y: WCHAN      = Sleeping in Function

As you can see from that output, you can sort the top command output by many different pieces of process information. The asterisk indicates that the output is currently sorted by the %CPU column.

Linux and Unix top command - closing notes

To exit/quit the top command, just type the letter q.

The top command implementation is pretty different on Mac OS X. The top command itself brings up a screen, but the columns are different by default, and the commands you can issue are different. I'm not going to get into all the details for Mac OS X, but just use the ? to get help.

You can control "summary" lines that are displayed on the top lines of the top command display. Use the following one-letter commands to toggle the display of the lines at the top of the display:

  • l - load average
  • m - memory
  • t - task/cpu stats

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值