10 Basic examples of PS commands under Linux

Source: Internet
Author: User
Tags fpm memory usage sort cpu usage

Under Linux, the PS command is one of the most basic commands for viewing processes running on a system. It provides the current process at the same time, such as user id,cpu usage, memory usage, command name, and so on, it does not display real-time data, such as the top or htop command details. However, even if the functionality and output is simple, but it is still a required process management/monitoring tool, every Linux novice should know this, study hard. In this article, we will use the PS command to view processes, filter, and sort them in different ways to strengthen the base section.

Syntax Note:

The PS command comes in 2 different styles, BSD and UNIX, respectively. New users often confuse and incorrectly interpret the two styles. So to figure them out, here are some basic information before proceeding.

Note: "PS aux" and "ps-aux" are not the same. For example, "-u" is used to display the process for that user. But "U" is the display of detailed information.

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

PS aux

Unix/linux style: There is a dash in front of the Linux-style grammar options as usual. ...

Ps-ef is a good thing to mix the syntax style on both Linux systems. such as "PS Ax-f". But in this article, we'll focus on UNIX-style syntax.

How do I use the PS command?

1. Show All Processes:

The following command lists all the processes:

The code is as follows:

$ ps AX

$ ps-ef

Plus pipe output to less, to scroll the display

' U ' or '-f ' parameter to show details of all processes

The code is as follows:

___fckpd___0nbsp;ps aux

___fckpd___0nbsp;ps-ef-f

Note: Why the user column does not display my username, but displays other users, such as root, www, and so on, for all user names (including you) if the length is longer than 8 characters, then PS will display only the UID instead of the user name.

2, according to the user to show the process:

Displayed by the user who owns the process with the "-u" option followed by the user name. Multiple user names can be provided in comma-delimited form.

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. Show process by name and process ID:

Search for a process 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, according to the CPU or memory sorting:

The system administrator often wants to identify processes that consume large amounts of memory or CPU. The sort options let the list of processes be sorted based on specific fields or parameters.

Multiple fields separated by commas can be specified by the "–sort" option. In addition, the field can be prefixed with a "-" or "" symbol that represents descending or ascending sorting. There are many parameters to sort through the process list, and you can check the complete list of the man pages.

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

Displaying the top 5 consumes most of the CPU process.

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, with the style of the tree to show the process of hierarchical relations:

Many processes are actually branches of the parent process, knowing that the parent-child process relationship is often useful. The ' –forest ' option will establish a tree view of the ASCII art-style hierarchy.

The following command Apache2 the search process name to form 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 to show together because they use a different order.

6, display a child process of a parent process:

Here's an example that shows 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 "The", "is owned" by "is" main apache2 process and all other apache2 processes have been forked out O F 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, the thread that displays a process:

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

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

The code is as follows:

___fckpd___7nbsp;ps-p 3150-l

8, change the columns to display:

The PS command can be configured to display only the selected list. You can view the manual in order to display a complete list.

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

The code is as follows:

___FCKPD___8NBSP;PS-E-O Pid,uname,pcpu,pmem,comm

You can rename a column label, 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

Ten root 0.0 0.0 RCUOB/1

9, show the process running time:

Represents the running time of a process. For running times, 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

10, the PS command into a real-time viewer:

As always, the watch command can be used to capture the PS display process in real time. The 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 '

The output is like this on the desktop:

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 is refreshed and the statistics are refreshed every 1 seconds. But do not think that this is similar to the above.

You will notice that the output of the Top/htop command is more frequent than in the case.

This is because it outputs a combination of various values, CPU usage, and memory usage. But the above PS command sort display is simpler, taking a time of 1 columns (such as school math). So it doesn't update as fast as top.

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.