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 use the ps command under Linux

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

Share

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

Xiaobian to share with you how to use the ps command under Linux, I believe most people do not know how to use it, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

The ps command is one of the most basic commands for viewing processes running on your system under Linux. It provides the details of the current process, such as user ID, CPU usage, memory usage, command name, etc. It does not display real-time data such as top or htop commands. However, even though its features and output are simple, it is still a must-use process management/monitoring tool, and every Linux novice should know this and learn well. In this article, we'll use the ps command to view processes, filter and sort them in different ways to reinforce the basics.

Grammar Note:

The ps command comes in two different styles, BSD and UNIX. New users often confuse and misinterpret these two styles. So to figure them out, here's some basic information before moving on.

Note: "ps aux" and "ps -aux" are not the same. For example,"-u" is used to show the user's progress. But the "u" shows detailed information.

BSD-style: No hyphen before BSD-style syntax options.

ps aux

UNIX/LINUX Style: There is a dash in front of linux style syntax options as usual.

ps -ef Mixing syntax styles on two Linux systems is a good thing. For example,"ps ax -f." But in this article, we'll focus primarily on UNIX-style syntax.

How to use ps command?

1. Show all processes:

The following command lists all processes:

The code is as follows:

$ ps ax

$ ps -ef

Add pipe output to less to scroll the display

"u" or "-f" parameter to display details of all processes

The code is as follows:

___FCKpd___0nbsp;ps aux

___FCKpd___0nbsp;ps -ef -f

Note: Why does the user column not show my username but show other users such as root, www, etc. For all usernames (including you) if the length is greater than 8 characters then ps will only show UID and not username.

2, according to the user display process:

Displayed by the user to whom the process belongs using the "-u" option followed by the username. Multiple user names can be provided separated by commas.

The code is as follows:

___FCKpd___1nbsp;ps -f -u www-data

UID PID PPID C STIME TTY TIME CMD

www-data 1329 1328 0 09:32 ? 00:00:00 nginx: worker process

www-data 1330 1328 0 09:32 ? 00:00:00 nginx: worker process

www-data 1332 1328 0 09:32 ? 00:00:00 nginx: worker process

www-data 1377 1372 0 09:32 ? 00:00:00 php-fpm: pool a.localhost

www-data 1378 1372 0 09:32 ? 00:00:00 php-fpm: pool a.localhost

www-data 4524 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k start

www-data 4527 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k start

www-data 4528 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k start

3. Display processes by name and process ID:

Search for processes by name or command, using the "-C" option followed by the search term.

The code is as follows:

___FCKpd___2nbsp;ps -C apache2

PID TTY TIME CMD

2359 ? 00:00:00 apache2

4524 ? 00:00:00 apache2

4525 ? 00:00:00 apache2

...

4. Sort by CPU or memory:

System administrators often want to identify processes that consume a lot of memory or CPU. The sort option sorts the list of processes based on specific fields or parameters.

Multiple fields separated by commas can be specified with the â € "sort â €" option. In addition, the field can be prefixed with a "-" or "" symbol to indicate descending or ascending sorting, respectively. Sorting by process list has many parameters, you can check the man page for a complete list.

___FCKpd___3nbsp;ps aux --sort=-pcpu,+pmem

Shows the top 5 processes that consume most CPU.

The code is as follows:

___FCKpd___4nbsp;ps aux --sort=-pcpu | head -5

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 2.6 0.7 51396 7644 ? Ss 02:02 0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 23

root 1249 2.6 3.0 355800 30896 tty1 Rsl+ 02:02 0:02 /usr/bin/X -background none :0 vt01 -nolisten tcp

root 508 2.4 1.6 248488 16776 ? Ss 02:02 0:03 /usr/bin/python /usr/sbin/firewalld --nofork

silver 1525 2.1 2.3 448568 24392 ? S 02:03 0:01 /usr/bin/python /usr/share/system-config-printer/applet.py

5. Show the hierarchical relationship of the process in the style of tree:

Many processes are actually offshoots of some parent process, and it is often useful to know that this parent-child relationship exists. The '-forest' option will create a tree view of ASCII art hierarchy.

The following command names the search process Apache2, forming a tree structure to display detailed information.

The code is as follows:

___FCKpd___5nbsp;ps -f --forest -C apache2

UID PID PPID C STIME TTY TIME CMD

root 2359 1 0 09:32 ? 00:00:00 /usr/sbin/apache2 -k start

www-data 4524 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start

www-data 4525 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start

www-data 4526 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start

www-data 4527 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start

www-data 4528 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start

Try not to use any sort and tree style together because they use a different order.

Show child processes of a parent process:

Here's an example showing all the branches of the apache process

The code is as follows:

___FCKpd___6nbsp;ps -o pid,uname,comm -C apache2

PID USER COMMAND

2359 root apache2

4524 www-data apache2

4525 www-data apache2

4526 www-data apache2

4527 www-data apache2

4528 www-data apache2

[term]

The first process that is owned by root is the main apache2 process and all other apache2 processes have been forked out of this main process. The next command lists all child apache2 processes using the pid of the main apache2 process

[term]

___FCKpd___6nbsp;ps --ppid 2359

PID TTY TIME CMD

4524 ? 00:00:00 apache2

4525 ? 00:00:00 apache2

4526 ? 00:00:00 apache2

4527 ? 00:00:00 apache2

4528 ? 00:00:00 apache2

7. Show the thread of a process:

The "-L" option displays the threads of the process. It can be used to display all threads or all processes of a particular process.

The following command displays all threads owned by processes with id 3150.

The code is as follows:

___FCKpd___7nbsp;ps -p 3150 -L

8. Change the columns to be displayed:

The ps command can be configured to display only selected lists. For a complete list, see the manual.

The following commands show only columns for PID, username, CPU, memory, and commands.

The code is as follows:

___FCKpd___8nbsp;ps -e -o pid,uname,pcpu,pmem,comm

Column labels can be renamed, which is quite flexible.

The code is as follows:

___FCKpd___9nbsp;ps -e -o pid,uname=USERNAME,pcpu=CPU_USAGE,pmem,comm

PID USERNAME CPU_USAGE %MEM COMMAND

1 root 0.0 0.0 init

2 root 0.0 0.0 kthreadd

3 root 0.0 0.0 ksoftirqd/0

4 root 0.0 0.0 kworker/0:0

5 root 0.0 0.0 kworker/0:0H

7 root 0.0 0.0 migration/0

8 root 0.0 0.0 rcu_bh

9 root 0.0 0.0 rcuob/0

10 root 0.0 0.0 rcuob/1

9. Display the running time of the process:

Represents the elapsed time of the process. For elapsed time, columns are not displayed by default and can be viewed using the "-O" option.

The code is as follows:

___FCKpd___10nbsp;ps -e -o pid,comm,etime

Turn the ps command into a real-time viewer:

As usual, the watch command can be used to capture ps display progress in real time. A simple example is as follows:

The code is as follows:

___FCKpd___11nbsp;watch -n 1 'ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15'

Output on the desktop looks like this:

The code is as follows:

Every 1.0s: ps -e -o pid,uname,cmd,pmem,pcpu --... Sun Dec 1 18:16:08 2013

PID USER CMD %MEM %CPU

3800 1000 /opt/google/chrome/chrome - 4.6 1.4

7492 1000 /opt/google/chrome/chrome - 2.7 1.4

3150 1000 /opt/google/chrome/chrome 2.7 2.5

3824 1000 /opt/google/chrome/chrome - 2.6 0.6

3936 1000 /opt/google/chrome/chrome - 2.4 1.6

2936 1000 /usr/bin/plasma-desktop 2.3 0.2

9666 1000 /opt/google/chrome/chrome - 2.1 0.8

3842 1000 /opt/google/chrome/chrome - 2.1 0.8

4739 1000 /opt/google/chrome/chrome - 1.8 1.0

3930 1000 /opt/google/chrome/chrome - 1.7 1.0

3911 1000 /opt/google/chrome/chrome - 1.6 0.6

3645 1000 /opt/google/chrome/chrome - 1.5 0.4

3677 1000 /opt/google/chrome/chrome - 1.5 0.4

3639 1000 /opt/google/chrome/chrome - 1.4 0.4

The output will be refreshed, updating statistics every 1 second. Don't think it's similar to above.

You'll notice that the output of the top/htop command changes more frequently than in other cases.

This is because the above outputs various combinations of values,CPU usage and memory usage. But the ps command sort shown above is simpler and takes one column at a time (such as school math). So it doesn't update as quickly as top.

The above is "Linux ps command how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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