IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> linux top命令 使用大全 -> 正文阅读

[系统运维]linux top命令 使用大全

一、Introduction

The top (table of processes) command shows a real-time view of running processes in Linux and displays kernel-managed tasks. The command also provides a system information summary that shows resource utilization, including CPU and memory usage.

In this tutorial, you will learn to use the top command in Linux.

Prerequisites
A system running Linux.
Access to a terminal window.
Administrator/sudo privileges.

二、top Command Syntax and Options

To run the top command, type top in the command line and press Enter. The command starts in interactive command mode, showing the active processes and other system information. Customize the view using the available options.

Below is the top command syntax with all the available options and flags:

top -hv | -bcEeHiOSs1 -d secs -n max -u | U user -p pid(s) -o field -w [cols]

The switch (-) and whitespace characters are optional.

OptionNameDescription
-hHelpShows the command help file.
-vVersionShows the library version and usage prompt and then quits the program
-bBatch modeStarts top in batch mode, allowing to send the command’s output to a file or other programs. top doesn’t accept input in batch mode and runs until killed or until it reaches the specified iteration limit.
-cToggle(切换) command line/program nameShows the command help file.
-d [secs.tenths]Delay time intervalPrompts you to specify the delay before updating the screen, overriding the default value. The value is in seconds. The command accepts fractional seconds but doesn’t accept negative numbers.
-e [kmg
-E [kmg
-HThreads mode operationSpecifying the -H option instructs top to display individual threads instead of a sum of all threads in each process.
-iIdle process toggleReverses the last remembered i state. When the toggle is OFF, top doesn’t display the tasks that haven’t used CPU resources since the last update.
-nLimit iteration numberSpecify the maximum number of iterations you want top to produce before ending.
-o [field name]Overwrite sort fieldSpecify the field name top uses to sort tasks. Prepending a + to the field name forces top to sort high to low while prepending it with - orders tasks low to high. The -o option primarily supports automated batch mode operation.
-OOutput field namesThe -O option complements the -o option, causing top to print each of the available field names separately and then quit.
-p [N1, N2…]Monitor process IDs modeInstructs top to monitor only the processes with the specified process IDs. When combined with the threads mode (-H), top shows all the processes in each monitored PID thread group. The maximum number of PIDs is 20. Specifying the PID value 0 is considered as the top program process ID. To return to normal operation, issue any of the following commands: =, u, or U, as they are mutually exclusive.
-sSecure mode operationStarts top in secure mode, even for root.
-SCumulative time toggleReverses the last remembered S state. When the cumulative time toggle is ON, each process is listed with the CPU time it has used.
-u-U [ID or name]User filter mode
-w [number]Output width overrideWhen used without an argument, -w instructs top to format the output using the COLUMNS= and LINES= environment variables, if applicable. Otherwise, the width is fixed at 512 columns maximum. Appending an argument to -w increases (up to 512) or decreases the output width while the row number is unlimited. The program never exceeds the actual terminal dimensions at which top was invoked.
-1Single/separate CPU states toggleReverses the last remembered CPU state portion. Depending on the NUMA Node command toggle (2) state, either displays all CPU information in a single line or separately.

三、top Command Examples

1、List All Processes

To list all running Linux processes on your system, open a terminal and enter:

top

在这里插入图片描述
The output displays the summary area (the dashboard with resource usage stats) and the task area (a list of all processes). top updates the information every three seconds by default.

If the process list is long, scroll through it using the Up and Down arrows and Page Up and Page Down keys. To quit top, press q.

2、Send a Signal

Use the top command to send any signal to a running process. Press the k key and enter the process PID. top gives you a chance to type the signal you want to send. Not entering a specific signal kills the process.

For example:
在这里插入图片描述
Here, we killed the mysql process 1207.

3、Exit After X Repetitions

The top command keeps refreshing the statistics until quitting the program by pressing q. Instruct top to quit automatically after refreshing the stats for a specified number of times.

The syntax is:

top -n [X]

For [X], specify the number of times you want top to refresh the output.

4、Sort Processes

By default, top sorts the process list using the %CPU column. To sort processes using a different column, press one of the following keys:

  • M. Sort by the %MEM column.
  • N. Sort by PID column.
  • T. Sort by the TIME+ column.
  • P. Sort by the %CPU column.
    In the following example, the process list is sorted by the %MEM column:
    在这里插入图片描述
    Note: Make the settings and customizations persistent by pressing the W key. The changes will be in effect the next time you start top. The configurations are kept in the .toprc file in your home directory.

5、Filter Processes by Specific User

The -u option allows you to display all user-specific processes. Press the u key while top is running or use the following syntax:

top -u [user name]

Pressing the u key in interactive mode prompts for the user name or user ID.

For example:
在这里插入图片描述In the example above, the output shows only processes for the root user.

6、Change Display Units

Use the E key to choose the units you want top to use for displaying the memory values. Press lowercase e to choose the units for displaying values in the process list. The available options are:

  • kibibytes (KiB)
  • mebibytes (MiB)
  • gibibytes (GiB)
  • tebibytes (TiB)
  • pebibytes (PiB)
  • exbibytes (EiB - applies only for memory values)
    The value that is currently in use is the first item on lines four and five.

In the following example, we have set the dashboard memory units to gibibytes, and the process list memory units to mebibytes:
在这里插入图片描述

7、Change Output Contents

The default top output contains a lot of information and may appear confusing. Tune the contents with a few keypresses, find the information you need, or delete certain sections from the summary.

7.1 Individual CPU Core Statistics

Pressing 1 on your keyboard allows you to see individual statistics for each CPU core. Press 1 repeatedly to toggle core statistics.

在这里插入图片描述

7.2 CPU Usage Graph

If you want a graphical representation of CPU usage, top supports elementary ASCII graphs, denoting each CPU core usage. Toggle the graphs on or off by pressing t.
在这里插入图片描述To change the graph display to solid block characters, press t again.
在这里插入图片描述
Press t again to remove the CPU display and task summary section.

7.3 Memory Usage Graph

The command also supports graphical display options of memory and swap memory lines. To get a visual representation of memory usage, press m. Remove the lines by pressing m again.
在这里插入图片描述

8、Change Text Color

Emphasize the information in the output by adding color to the output. Press the z key to add color to the display:
在这里插入图片描述
By default, the color changes to red. To further customize display elements and choose a different color, follow the steps below:

  1. Open the color settings page by pressing Z.
    在这里插入图片描述2. Choose which display element you want to change by pressing:
  • S. Summary Data area.
  • M. Messages and prompts.
  • H. Column headings.
  • T. Task information in the process list.
    Press Enter to confirm your choice.
    3.Pick the color for the selected element with one of the following keys:
    1. Black.
    1. Red.
    1. Green.
    1. Yellow.
    1. Blue.
    1. Magenta.
    1. Cyan.
    1. White.
      Confirm your choice with Enter.
      Now test the new settings:
      在这里插入图片描述
      We instructed top to print the process list in green, as you see in the example above.

9、Show Process Command Line

To see the full command line instead of the process name, press the c key. To toggle back to displaying the process name, press c again.

在这里插入图片描述

10、See Process Hierarchy

To see which processes were launched or spawned by other processes, press the V key.
在这里插入图片描述The tree view is useful when you want to see the child-parent process hierarchy on your system.

11、Show Active Tasks

To filter the output and show only active tasks, press the I key.
在这里插入图片描述
The output hides the tasks that haven’t used any CPU resources since the last update.

12、Limit Process Number

Press the n key to limit the process number in the output. Regardless of the number of active processes. top prompts you to enter the number of processes you want to see.

For example, we want to show only the top 5 processes:

在这里插入图片描述

13、Change Process Priority

The top command allows changing the process priority (nice value).

Follow these steps:

  1. Run top and press the r key.

  2. When prompted, enter the process ID and press Enter.

  3. After you enter the PID, the program prompts for a new nice value. Enter a new value and press Enter.

For example, we have set a nice value of 10 to a VBoxClient process instance:

在这里插入图片描述

14、Filter Processes

The top filter feature allows using a filter expression to limit which processes to see in the list. Activate the filter option by pressing the o key. The program prompts you to enter a filter expression.

For example, we entered the following filter:

%MEM>5.0

在这里插入图片描述

四、结论

The top command is a must when trying to identify a problem and deal with it before it affects your system. The dashboard provides an abundance of system information and metrics that help you control running processes, while the various options allow you to find and resolve any possible issues quickly.

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-05-26 15:25:35  更:2022-05-26 15:26:14 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 14:03:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码