10 basic examples of ps commands in Linux

Source: Internet
Author: User
In Linux, the ps command is one of the most basic commands used to view processes running on the system. In this article, we will use the ps command to view processes, filter and sort them in different ways to enhance the basics

In Linux, the ps command is one of the most basic commands used to view processes running on the system. It provides the current process, such as user ID, CPU usage, memory usage, and command name. it does not display real-time data, such as the top or htop command details. However, even if the functions and outputs are simple, it is still a required process management/monitoring tool. every Linux beginner should know this and learn it well. In this article, we will use the ps command to view processes, filter and sort them in different ways to enhance the basics.

Syntax note:

Ps commands have two different styles: BSD and UNIX. New users often confuse and incorrectly interpret these two styles. So we need to figure out some basic information before proceeding.

Note: "ps aux" and "ps-aux" are different. For example, "-u" is used to display the user's process. However, "u" displays detailed information.

BSD style: the BSD style syntax options do not contain hyphens.

Ps aux

UNIX/LINUX style: there is a break in front of the syntax option in linux style ....

Ps-ef is a good thing to mix the syntax styles on two Linux systems. For example, "ps ax-f ". However, in this article, we will focus on UNIX-style syntax.

How to use the ps command?

1. display all processes:

The following command lists all processes:


Copy codeThe code is as follows:
$ Ps ax
$ Ps-ef

Add MPs queue output to less to Scroll Display

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


Copy codeThe code is as follows:
___ FCKpd ___ 0 nbsp; ps aux
___ FCKpd ___ 0 nbsp; ps-ef-f

Note: Why is my user name not displayed in the User column, but other users, such as root and www, displayed? if the length of all user names (including you) is greater than 8 characters, then ps will only display UID, not the user name.

2. display processes based on the User:

The "-u" option is used by the process owner and displayed by the user name. Multiple user names can be separated by commas.


Copy codeThe code is as follows:
___ FCKpd ___ 1 nbsp; ps-f-u www-data
UID PID PPID C STIME TTY TIME CMD
Www-data 1329 1328 0? 00:00:00 nginx: worker process
Www-data 1330 1328 0? 00:00:00 nginx: worker process
Www-data 1332 1328 0? 00:00:00 nginx: worker process
Www-data 1377 1372 0? 00:00:00 php-fpm: pool a. localhost
Www-data 1378 1372 0? 00:00:00 php-fpm: pool a. localhost
Www-data 4524 2359 0? 00:00:00/usr/sbin/apache2-k start
Www-data 4527 2359 0? 00:00:00/usr/sbin/apache2-k start
Www-data 4528 2359 0? 00:00:00/usr/sbin/apache2-k start

3. display the process by name and process ID:

Search for a process by name or command, and use the "-C" option followed by a search term.


Copy codeThe code is as follows:
___ FCKpd ___ 2 nbsp; ps-C apache2
PID TTY TIME CMD
2359? Apache2 00:00:00
4524? Apache2 00:00:00
4525? Apache2 00:00:00
...

4. Sort by CPU or Memory:

The system administrator often wants to find out the processes that consume a large amount of memory or CPU. The sorting option sorts the process list based on specific fields or parameters.

The "-sort" option can be specified for multiple fields separated by commas. In addition, this field can contain the prefix "-" or "", indicating that the fields are sorted in descending or ascending order. There are many parameters for sorting through the process List. you can check the complete list on the manual page.

___ FCKpd ___ 3 nbsp; ps aux -- sort =-pcpu, + pmem
It indicates that the first five CPU processes consumed most of the time.


Copy codeThe code is as follows:
___ FCKpd ___ 4 nbsp; 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/usr/lib/systemd -- switched-root -- system -- deserialize 23
Root 1249 2.6 3.0 355800 30896 tty1 Rsl +/usr/bin/X-background none: 0 vt01-nolisten tcp
Root 508 2.4 1.6 248488 16776? Ss/usr/bin/python/usr/sbin/firewalld -- nofork
Silver 1525 2.1 2.3 448568 24392? S/usr/bin/python/usr/share/system-config-printer/applet. py

5. display the process hierarchy in the tree style:

Many processes are actually the branches of some parent processes. it is often useful to know the parent-child process relationship. The '-forest' option creates a tree view of the ASCII artistic style hierarchy.

The following command searches for a process named Apache2 to form a tree structure to display detailed information.


Copy codeThe code is as follows:
___ FCKpd ___ 5 nbsp; 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? 00:00:00 \ _/usr/sbin/apache2-k start
Www-data 4525 2359 0? 00:00:00 \ _/usr/sbin/apache2-k start
Www-data 4526 2359 0? 00:00:00 \ _/usr/sbin/apache2-k start
Www-data 4527 2359 0? 00:00:00 \ _/usr/sbin/apache2-k start
Www-data 4528 2359 0? 00:00:00 \ _/usr/sbin/apache2-k start

Try not to use any sort and tree style for display, because they use different order.

6. display the child process of a parent process:

Here is an example showing the branches of all apache processes


Copy codeThe code is as follows:
___ FCKpd ___ 6 nbsp; 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 ___ 6 nbsp; ps -- ppid 2359
PID TTY TIME CMD
4524? Apache2 00:00:00
4525? Apache2 00:00:00
4526? Apache2 00:00:00
4527? Apache2 00:00:00
4528? Apache2 00:00:00

7. display the thread of a process:

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

The following command displays the threads of all processes with id 3150.

Copy codeThe code is as follows:
___ FCKpd ___ 7 nbsp; ps-p 3150-L

8. change the columns to be displayed:

The ps command can be configured to display only the selected list. To display the complete list, you can view the manual.

The following command only displays the PID, user name, CPU, memory, and command columns.


Copy codeThe code is as follows:
___ FCKpd ___ 8 nbsp; ps-e-o pid, uname, pcpu, pmem, comm

You can rename the column label, which is quite flexible.


Copy codeThe code is as follows:
___ FCKpd ___ 9 nbsp; 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: 0 H
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:

Indicates the running time of the process. For the running time, the column is not displayed by default. you can use the "-O" option to view it.

Copy codeThe code is as follows:
___ FCKpd ___ 10 nbsp; ps-e-o pid, comm, etime

10. convert the ps command into a real-time viewer:

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


Copy codeThe code is as follows:
___ FCKpd ___ 11 nbsp; watch-n 1' ps-e-o pid, uname, cmd, pmem, pcpu -- sort =-pmem,-pcpu | head-15'

Output on the desktop like this:


Copy codeThe code is as follows:
Every 1.0 s: 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-4.6 1.4
7492 1000/opt/google/chrome-2.7 1.4
3150 1000/opt/google/chrome 2.7 2.5
3824 1000/opt/google/chrome-2.6 0.6
3936 1000/opt/google/chrome-2.4 1.6
2936 1000/usr/bin/plasma-desktop 2.3 0.2
9666 1000/opt/google/chrome-2.1 0.8
3842 1000/opt/google/chrome-2.1 0.8
4739 1000/opt/google/chrome-1.8 1.0
3930 1000/opt/google/chrome-1.7 1.0
3911 1000/opt/google/chrome-1.6 0.6
3645 1000/opt/google/chrome-1.5 0.4
3677 1000/opt/google/chrome-1.5 0.4
3639 1000/opt/google/chrome-1.4 0.4

The output is refreshed and the statistics are refreshed every second. But do not think this is similar to the above.

You will notice that the output of the top/htop command changes more frequently.

This is because the above output a variety of values, a combination of CPU usage and memory usage. However, the above ps command sorting display is simpler, taking a time column (such as school mathematics), so it will not be updated as quickly as top.

Link: http://www.21ops.com/linux/5623.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.