Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to sort using the output of ps command in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use ps command output for sorting in Linux, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

The ps command is the key to understanding what runs on a Linux system and the resources used by each process. It's useful to know how to display the information PS provides in any way that helps you focus on the problem you're trying to solve. One aspect of this is the ability to sort the output of ps aux commands by any column to highlight certain information, such as how much memory a process uses or how long it has been running.

Tips include using the ps command's--sort option and knowing how to specify columns to use for sorting. By default, ps is sorted by process id (PID), showing the smallest first. PID 1 will appear at the top of the list, directly below the column heading. The rest will be arranged in numerical order.

Here is an example of a standard ps-aux output. It might look familiar.

linuxmi@linuxmi:~/www.linuxmi.com$ ps aux | head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 171788 16056 ? Ss 08:59 0:05 /sbin/init splash root 2 0.0 0.0 0 0 ? S 08:59 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I

< 08:59 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 08:59 0:00 [rcu_par_gp]

With the--sort option, you get the same output if you sort by PID. Note that you need to enter "pid" in lowercase. However, you can change pid to any other column and sort by that column.

linuxmi@linuxmi:~/www.linuxmi.com$ ps aux --sort pid | head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 171788 16056 ? Ss 08:59 0:05 /sbin/init splash root 2 0.0 0.0 0 0 ? S 08:59 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 08:59 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 08:59 0:00 [rcu_par_gp]

To see the maximum value of the selected column listed first, you must change the sort option (for example, replace pid with-pid). Notice how, in this case, it shows how to start with the highest process ID. Think of-as a descending value (maximum first), while names (such as pid) or names with a plus sign (such as +pid) indicate ascending. The commands shown below are sorted by process ID, with the largest displayed first.

linuxmi@linuxmi:~/www.linuxmi.com$ ps aux --sort -pid | head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND linuxmi 38579 0.0 0.0 11368 596 pts/2 S+ 13:33 0:00 head -5 linuxmi 38578 0.0 0.0 15124 3788 pts/2 R+ 13:33 0:00 ps aux --sort -pid linuxmi 38571 0.2 0.1 13960 5240 pts/2 Ss 13:32 0:00 bash root 38546 0.0 0.0 0 0 ? I 13:32 0:00 [kworker/0:3]

To sort by other columns, you need to use their column names. In this example," pmem" represents the percentage of memory usage, and using " -pmem" means we'll look at the processes that use the most memory first. You can also use "%mem" and "-%mem."

linuxmi@linuxmi:~/www.linuxmi.com$ ps aux --sort -pmem | head -5 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND mysql 1559 0.2 8.9 1282972 357304 ? Ssl 08:59 0:34 /usr/sbin/mysqld linuxmi 23618 0.1 8.4 3424800 338068 ? Ssl 09:29 0:28 /usr/bin/gnome-shell linuxmi 25474 0.0 7.1 971780 286636 ? Sl 09:30 0:07 /usr/bin/gnome-software --gapplication-service linuxmi 23920 0.0 5.7 1143616 228824 ? Sl 09:29 0:08 /snap/snap-store/467/usr/bin/snap-store --gapplication-service

If you want to see only the select column of ps output, you can specify in this example that only commands and their CPU usage are displayed, and the output is sorted by maximum CPU usage.

linuxmi@linuxmi:~/www.linuxmi.com$ ps -eo comm,pcpu --sort -pcpu | head -5 COMMAND %CPU tracker-extract 4.6 systemd-hostnam 1.8 netdata 1.7 apps.plugin 1.7

The following is a list of column names and strings that can be used to sort them:

Column Ascending Descending Heading Sort Sort Alternatives =============================================== USER user -user PID pid -pid %CPU pcpu -pcpu %cpu and -%cpu %MEM pmem -pmem %pmem and -%pmem VSZ vsz -vsz RSS rss -rss TTY tty -tty STAT stat -stat START start -start TIME time -time COMMAND comm -comm

Using the sort command

You can also pipe the output of the ps aux command to the sort command and use the column numbers (1 through 11) to select the columns to use for sorting. This approach has two drawbacks: 1) column headings will sort along with the rest of ps output and may end up where you don't want to see them;2) when sorting requires numeric values, you need to add an "n" to the sort command. The ps command understands this distinction; commands like this sort by memory usage. The sort command uses "-nk 4" to sort numerically the (-k 4) 4th data column in the sort output.

linuxmi@linuxmi:~/www.linuxmi.com$ ps aux | head -1; ps aux | sort -r -nk 4 | head -3 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND mysql 1559 0.2 8.9 1282972 357304 ? Ssl 08:59 0:35 /usr/sbin/mysqld linuxmi 23618 0.2 8.6 3432704 346044 ? Ssl 09:29 0:38 /usr/bin/gnome-shell linuxmi 25474 0.0 7.1 971780 286636 ? Sl 09:30 0:07 /usr/bin/gnome-software --gapplication-service

use aliases

Even if the ps command is flexible enough for your needs, you can always create aliases so that you don't have to count columns and/or consider whether column values are numbers. Here are some examples:

alias LmCPU='ps aux --sort -%cpu' alias LmMem='ps aux --sort -%mem' alias TopCPU='ps aux --sort -%cpu | head -11' alias TopMem='ps aux --sort -%mem |head -11'Thank you for reading this article carefully. I hope that Xiaobian's shared article "How to use ps command output for sorting in Linux" will help everyone. At the same time, I hope that everyone will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report