如何在批处理模式下运行top命令

系统 Linux
top 命令 是每个人都在使用的用于 监控 Linux 系统性能 的最好的命令。你可能已经知道 top 命令的绝大部分操作,除了很少的几个操作,如果我没错的话,批处理模式就是其中之一。

[[279987]]

top 命令 是每个人都在使用的用于 监控 Linux 系统性能 的最好的命令。你可能已经知道 top 命令的绝大部分操作,除了很少的几个操作,如果我没错的话,批处理模式就是其中之一。

大部分的脚本编写者和开发人员都知道这个,因为这个操作主要就是用来编写脚本。

如果你不了解这个,不用担心,我们将在这里介绍它。

什么是 top 命令的批处理模式

批处理模式允许你将 top 命令的输出发送至其他程序或者文件中。

在这个模式中,top 命令将不会接收输入并且持续运行,直到迭代次数达到你用 -n 选项指定的次数为止。

如果你想解决 Linux 服务器上的任何性能问题,你需要正确的 理解 top 命令的输出

1) 如何在批处理模式下运行 top 命令

默认地,top 命令按照 CPU 的使用率来排序输出结果,所以当你在批处理模式中运行以下命令时,它会执行同样的操作并打印前 35 行:

  1. # top -bc | head -35
  2.  
  3. top - 06:41:14 up 8 days, 20:24, 1 user, load average: 0.87, 0.77, 0.81
  4. Tasks: 139 total, 1 running, 136 sleeping, 0 stopped, 2 zombie
  5. %Cpu(s): 0.0 us, 3.2 sy, 0.0 ni, 96.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
  6. KiB Mem : 3880940 total, 1595932 free, 886736 used, 1398272 buff/cache
  7. KiB Swap: 1048572 total, 514640 free, 533932 used. 2648472 avail Mem
  8.  
  9. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  10. 1 root 20 0 191144 2800 1596 S 0.0 0.1 5:43.63 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
  11. 2 root 20 0 0 0 0 S 0.0 0.0 0:00.32 [kthreadd]
  12. 3 root 20 0 0 0 0 S 0.0 0.0 0:28.10 [ksoftirqd/0]
  13. 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/0:0H]
  14. 7 root rt 0 0 0 0 S 0.0 0.0 0:33.96 [migration/0]
  15. 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [rcu_bh]
  16. 9 root 20 0 0 0 0 S 0.0 0.0 63:05.12 [rcu_sched]
  17. 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [lru-add-drain]
  18. 11 root rt 0 0 0 0 S 0.0 0.0 0:08.79 [watchdog/0]
  19. 12 root rt 0 0 0 0 S 0.0 0.0 0:08.82 [watchdog/1]
  20. 13 root rt 0 0 0 0 S 0.0 0.0 0:44.27 [migration/1]
  21. 14 root 20 0 0 0 0 S 0.0 0.0 1:22.45 [ksoftirqd/1]
  22. 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/1:0H]
  23. 18 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [kdevtmpfs]
  24. 19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [netns]
  25. 20 root 20 0 0 0 0 S 0.0 0.0 0:01.35 [khungtaskd]
  26. 21 root 0 -20 0 0 0 S 0.0 0.0 0:00.02 [writeback]
  27. 22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kintegrityd]
  28. 23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [bioset]
  29. 24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kblockd]
  30. 25 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [md]
  31. 26 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [edac-poller]
  32. 33 root 20 0 0 0 0 S 0.0 0.0 1:19.07 [kswapd0]
  33. 34 root 25 5 0 0 0 S 0.0 0.0 0:00.00 [ksmd]
  34. 35 root 39 19 0 0 0 S 0.0 0.0 0:12.80 [khugepaged]
  35. 36 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [crypto]
  36. 44 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kthrotld]
  37. 46 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kmpath_rdacd]

2) 如何在批处理模式下运行 top 命令并按内存使用率排序结果

在批处理模式中运行以下命令按内存使用率对结果进行排序:

  1. # top -bc -o +%MEM | head -n 20
  2.  
  3. top - 06:42:00 up 8 days, 20:25, 1 user, load average: 0.66, 0.74, 0.80
  4. Tasks: 146 total, 1 running, 145 sleeping, 0 stopped, 0 zombie
  5. %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
  6. KiB Mem : 3880940 total, 1422044 free, 1059176 used, 1399720 buff/cache
  7. KiB Swap: 1048572 total, 514640 free, 533932 used. 2475984 avail Mem
  8.  
  9. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  10. 18105 mysql 20 0 1453900 156096 8816 S 0.0 4.0 2:12.98 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
  11. 1841 root 20 0 228980 107036 5360 S 0.0 2.8 0:05.56 /usr/local/cpanel/3rdparty/perl/528/bin/perl -T -w /usr/local/cpanel/3rdparty/bin/spamd --max-children=3 --max-spare=1 --allowed-ips=127.0.0.+
  12. 4301 root 20 0 230208 104608 1816 S 0.0 2.7 0:03.77 spamd child
  13. 8139 nobody 20 0 257000 27108 3408 S 0.0 0.7 0:00.04 /usr/sbin/httpd -k start
  14. 7961 nobody 20 0 256988 26912 3160 S 0.0 0.7 0:00.05 /usr/sbin/httpd -k start
  15. 8190 nobody 20 0 256976 26812 3140 S 0.0 0.7 0:00.05 /usr/sbin/httpd -k start
  16. 8353 nobody 20 0 256976 26812 3144 S 0.0 0.7 0:00.04 /usr/sbin/httpd -k start
  17. 8629 nobody 20 0 256856 26736 3108 S 0.0 0.7 0:00.02 /usr/sbin/httpd -k start
  18. 8636 nobody 20 0 256856 26712 3100 S 0.0 0.7 0:00.03 /usr/sbin/httpd -k start
  19. 8611 nobody 20 0 256844 25764 2228 S 0.0 0.7 0:00.01 /usr/sbin/httpd -k start
  20. 8451 nobody 20 0 256844 25760 2220 S 0.0 0.7 0:00.04 /usr/sbin/httpd -k start
  21. 8610 nobody 20 0 256844 25748 2224 S 0.0 0.7 0:00.01 /usr/sbin/httpd -k start
  22. 8632 nobody 20 0 256844 25744 2216 S 0.0 0.7 0:00.03 /usr/sbin/httpd -k start

上面命令的详细信息:

  • -b:批处理模式选项
  • -c:打印运行中的进程的绝对路径
  • -o:指定进行排序的字段
  • head:输出文件的第一部分
  • -n:打印前 n 行

3) 如何在批处理模式下运行 top 命令并按照指定的用户进程对结果进行排序

如果你想要按照指定用户进程对结果进行排序请运行以下命令:

  1. # top -bc -u mysql | head -n 10
  2.  
  3. top - 06:44:58 up 8 days, 20:27, 1 user, load average: 0.99, 0.87, 0.84
  4. Tasks: 140 total, 1 running, 137 sleeping, 0 stopped, 2 zombie
  5. %Cpu(s): 13.3 us, 3.3 sy, 0.0 ni, 83.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
  6. KiB Mem : 3880940 total, 1589832 free, 885648 used, 1405460 buff/cache
  7. KiB Swap: 1048572 total, 514640 free, 533932 used. 2649412 avail Mem
  8.  
  9. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  10. 18105 mysql 20 0 1453900 156888 8816 S 0.0 4.0 2:16.42 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

4) 如何在批处理模式下运行 top 命令并按照处理时间进行排序

在批处理模式中使用以下 top 命令按照处理时间对结果进行排序。这展示了任务从启动以来已使用的总 CPU 时间。

但是如果你想要检查一个进程在 Linux 上运行了多长时间请看接下来的文章:

  1. # top -bc -o TIME+ | head -n 20
  2.  
  3. top - 06:45:56 up 8 days, 20:28, 1 user, load average: 0.56, 0.77, 0.81
  4. Tasks: 148 total, 1 running, 146 sleeping, 0 stopped, 1 zombie
  5. %Cpu(s): 0.0 us, 3.1 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
  6. KiB Mem : 3880940 total, 1378664 free, 1094876 used, 1407400 buff/cache
  7. KiB Swap: 1048572 total, 514640 free, 533932 used. 2440332 avail Mem
  8.  
  9. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  10. 9 root 20 0 0 0 0 S 0.0 0.0 63:05.70 [rcu_sched]
  11. 272 root 20 0 0 0 0 S 0.0 0.0 16:12.13 [xfsaild/vda1]
  12. 3882 root 20 0 229832 6212 1220 S 0.0 0.2 9:00.84 /usr/sbin/httpd -k start
  13. 1 root 20 0 191144 2800 1596 S 0.0 0.1 5:43.75 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
  14. 3761 root 20 0 68784 9820 2048 S 0.0 0.3 5:09.67 tailwatchd
  15. 3529 root 20 0 404380 3472 2604 S 0.0 0.1 3:24.98 /usr/sbin/rsyslogd -n
  16. 3520 root 20 0 574208 572 164 S 0.0 0.0 3:07.74 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
  17. 444 dbus 20 0 58444 1144 612 S 0.0 0.0 2:23.90 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
  18. 18105 mysql 20 0 1453900 157152 8816 S 0.0 4.0 2:17.29 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
  19. 249 root 0 -20 0 0 0 S 0.0 0.0 1:28.83 [kworker/0:1H]
  20. 14 root 20 0 0 0 0 S 0.0 0.0 1:22.46 [ksoftirqd/1]
  21. 33 root 20 0 0 0 0 S 0.0 0.0 1:19.07 [kswapd0]
  22. 342 root 20 0 39472 2940 2752 S 0.0 0.1 1:18.17 /usr/lib/systemd/systemd-journald

5) 如何在批处理模式下运行 top 命令并将结果保存到文件中

如果出于解决问题的目的,你想要和别人分享 top 命令的输出,请使用以下命令重定向输出到文件中:

  1. # top -bc | head -35 > top-report.txt
  2.  
  3. # cat top-report.txt
  4.  
  5. top - 06:47:11 up 8 days, 20:30, 1 user, load average: 0.67, 0.77, 0.81
  6. Tasks: 133 total, 4 running, 129 sleeping, 0 stopped, 0 zombie
  7. %Cpu(s): 59.4 us, 12.5 sy, 0.0 ni, 28.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
  8. KiB Mem : 3880940 total, 1596268 free, 843284 used, 1441388 buff/cache
  9. KiB Swap: 1048572 total, 514640 free, 533932 used. 2659084 avail Mem
  10.  
  11. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  12. 9686 daygeekc 20 0 406132 62184 43448 R 94.1 1.6 0:00.34 /opt/cpanel/ea-php56/root/usr/bin/php-cgi
  13. 9689 nobody 20 0 256588 24428 1184 S 5.9 0.6 0:00.01 /usr/sbin/httpd -k start
  14. 1 root 20 0 191144 2800 1596 S 0.0 0.1 5:43.79 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
  15. 2 root 20 0 0 0 0 S 0.0 0.0 0:00.32 [kthreadd]
  16. 3 root 20 0 0 0 0 S 0.0 0.0 0:28.11 [ksoftirqd/0]
  17. 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/0:0H]
  18. 7 root rt 0 0 0 0 S 0.0 0.0 0:33.96 [migration/0]
  19. 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [rcu_bh]
  20. 9 root 20 0 0 0 0 R 0.0 0.0 63:05.82 [rcu_sched]
  21. 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [lru-add-drain]
  22. 11 root rt 0 0 0 0 S 0.0 0.0 0:08.79 [watchdog/0]
  23. 12 root rt 0 0 0 0 S 0.0 0.0 0:08.82 [watchdog/1]
  24. 13 root rt 0 0 0 0 S 0.0 0.0 0:44.28 [migration/1]
  25. 14 root 20 0 0 0 0 S 0.0 0.0 1:22.46 [ksoftirqd/1]
  26. 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/1:0H]
  27. 18 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [kdevtmpfs]
  28. 19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [netns]
  29. 20 root 20 0 0 0 0 S 0.0 0.0 0:01.35 [khungtaskd]
  30. 21 root 0 -20 0 0 0 S 0.0 0.0 0:00.02 [writeback]
  31. 22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kintegrityd]
  32. 23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [bioset]
  33. 24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kblockd]
  34. 25 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [md]
  35. 26 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [edac-poller]
  36. 33 root 20 0 0 0 0 S 0.0 0.0 1:19.07 [kswapd0]
  37. 34 root 25 5 0 0 0 S 0.0 0.0 0:00.00 [ksmd]
  38. 35 root 39 19 0 0 0 S 0.0 0.0 0:12.80 [khugepaged]
  39. 36 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [crypto]

如何按照指定字段对结果进行排序

top 命令的最新版本中, 按下 f 键进入字段管理界面。

要使用新字段进行排序, 请使用 up/down 箭头选择正确的选项,然后再按下 s 键进行排序。最后按 q 键退出此窗口。

  1. Fields Management for window 1:Def, whose current sort field is %CPU
  2. Navigate with Up/Dn, Right selects for move then or Left commits,
  3. 'd' or toggles display, 's' sets sort. Use 'q' or to end!
  4. PID = Process Id nsUTS = UTS namespace Inode
  5. USER = Effective User Name LXC = LXC container name
  6. PR = Priority RSan = RES Anonymous (KiB)
  7. NI = Nice Value RSfd = RES File-based (KiB)
  8. VIRT = Virtual Image (KiB) RSlk = RES Locked (KiB)
  9. RES = Resident Size (KiB) RSsh = RES Shared (KiB)
  10. SHR = Shared Memory (KiB) CGNAME = Control Group name
  11. S = Process Status NU = Last Used NUMA node
  12. %CPU = CPU Usage
  13. %MEM = Memory Usage (RES)
  14. TIME+ = CPU Time, hundredths
  15. COMMAND = Command Name/Line
  16. PPID = Parent Process pid
  17. UID = Effective User Id
  18. RUID = Real User Id
  19. RUSER = Real User Name
  20. SUID = Saved User Id
  21. SUSER = Saved User Name
  22. GID = Group Id
  23. GROUP = Group Name
  24. PGRP = Process Group Id
  25. TTY = Controlling Tty
  26. TPGID = Tty Process Grp Id
  27. SID = Session Id
  28. nTH = Number of Threads
  29. P = Last Used Cpu (SMP)
  30. TIME = CPU Time
  31. SWAP = Swapped Size (KiB)
  32. CODE = Code Size (KiB)
  33. DATA = Data+Stack (KiB)
  34. nMaj = Major Page Faults
  35. nMin = Minor Page Faults
  36. nDRT = Dirty Pages Count
  37. WCHAN = Sleeping in Function
  38. Flags = Task Flags
  39. CGROUPS = Control Groups
  40. SUPGIDS = Supp Groups IDs
  41. SUPGRPS = Supp Groups Names
  42. TGID = Thread Group Id
  43. OOMa = OOMEM Adjustment
  44. OOMs = OOMEM Score current
  45. ENVIRON = Environment vars
  46. vMj = Major Faults delta
  47. vMn = Minor Faults delta
  48. USED = Res+Swap Size (KiB)
  49. nsIPC = IPC namespace Inode
  50. nsMNT = MNT namespace Inode
  51. nsNET = NET namespace Inode
  52. nsPID = PID namespace Inode
  53. nsUSER = USER namespace Inode

top 命令的旧版本,请按 shift+fshift+o 键进入字段管理界面进行排序。

要使用新字段进行排序,请选择相应的排序字段字母, 然后按下回车键排序。

  1. Current Sort Field: N for window 1:Def
  2. Select sort field via field letter, type any other key to return
  3. a: PID = Process Id
  4. b: PPID = Parent Process Pid
  5. c: RUSER = Real user name
  6. d: UID = User Id
  7. e: USER = User Name
  8. f: GROUP = Group Name
  9. g: TTY = Controlling Tty
  10. h: PR = Priority
  11. i: NI = Nice value
  12. j: P = Last used cpu (SMP)
  13. k: %CPU = CPU usage
  14. l: TIME = CPU Time
  15. m: TIME+ = CPU Time, hundredths
  16. * N: %MEM = Memory usage (RES)
  17. o: VIRT = Virtual Image (kb)
  18. p: SWAP = Swapped size (kb)
  19. q: RES = Resident size (kb)
  20. r: CODE = Code size (kb)
  21. s: DATA = Data+Stack size (kb)
  22. t: SHR = Shared Mem size (kb)
  23. u: nFLT = Page Fault count
  24. v: nDRT = Dirty Pages count
  25. w: S = Process Status
  26. x: COMMAND = Command name/line
  27. y: WCHAN = Sleeping in Function
  28. z: Flags = Task Flags
  29. Note1:
  30. If a selected sort field can't be
  31. shown due to screen width or your
  32. field order, the '<' and '>' keys
  33. will be unavailable until a field
  34. within viewable range is chosen.
  35. Note2:
  36. Field sorting uses internal values,
  37. not those in column display. Thus,
  38. the TTY & WCHAN fields will violate
  39. strict ASCII collating sequence.
  40. (shame on you if WCHAN is chosen)

 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2019-06-26 08:00:39

Docker容器运行命令

2016-10-13 09:05:30

Windows 10DOS定时关机

2009-08-06 20:14:16

linux命令行模式linux命令行修改系统时间

2021-06-10 09:56:09

Linux命令shell

2017-05-08 18:10:54

2023-03-07 10:50:42

Linux命令系统

2011-07-27 17:21:34

安全模式光驱

2014-03-19 09:19:44

KDE应用GNOME

2020-01-14 11:45:31

安全模式Windows 10Windows

2022-01-11 07:50:32

Linux脚本模式

2015-01-27 23:21:53

Linux终端

2022-10-08 08:31:09

Linuxsudo

2019-12-04 12:28:24

TOP命令Linux

2014-05-04 10:58:18

Linux命令行Linux发行版

2017-02-06 18:00:26

Linuxsudo命令

2010-11-19 15:25:48

关闭Oracle服务

2022-04-01 12:51:44

命令Containerd

2018-07-02 09:00:27

Linux特定时间运行命令

2017-04-21 12:03:46

MacASP.NET Cor程序

2022-01-21 10:58:39

JavaScriptGolangPython
点赞
收藏

51CTO技术栈公众号