Linux under the PS command Usage summary

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

The estimation of Linux under the commonly used commands in the high usage of PS, today to translate this article PS related articles

Linux PS Command

The PS command on Linux are one of the most basic commands for viewing the processes running on the system. It provides a snapshot of the current processes along with detailed-like user ID, CPU information, usage memory, com Mand name etc. It does not display data in real time like the top or htop commands. But even though being simpler in features and output it is still a essential process management/monitoring tool that ever Y Linux newbie should know about and learn.

The PS command is the most basic command for Linux to view the processes running in the system. It provides current threads and related details such as user id,cpu occupancy, memory footprint, startup commands, and more. It's not the same as top or htop because it's not real-time. But it's output is simple, so it's a command that every operational officer needs to master.

In this post we are going to revise the basics of using the PS command to check the processes and filter and sort them in Different ways to suit better.

In this article, we'll talk about how to use PS to view the city and related filtering and sorting

Note on syntax

PS Syntax:

The PS command comes with a unusual set of 2 syntax styles. The is BSD and UNIX both. New users are often confused with and mis-interpret the two styles. So am some basic info to get it clear before moving on.

The PS command has two grammatical formats, namely BSD style and Unix style, and new users often feel troubled. So let's just say a little bit about the basics and move on.

Note: "PS aux" are not the same as "Ps-aux". For example "-u" is used to show process. But "U" means show detailed information.

Note: "PS aux" are not the same as "Ps-aux". For example "-u" is used to show process. But "U" means show detailed information.
Note: PS aux and ps-aux are not the same, for example-U is the one that shows the process, but u represents the details

BSD style–the options in the BSD style syntax are not preceded with a dash.

BSD: There is no--directly with the option

PS aux

Unix/linux style–the options in LINUX style syntax are preceded by a dash as usual.

Unix/linux: Just the back followed-and then with the option

Ps-ef

It is okay to mix both the syntax styles on Linux systems. For example "PS ax-f".

But in this post we shall mostly focus on the UNIX style syntax.
Use two style things at the same time, such as PS ax-f but our article is mostly Unix-style
It is okay to mix both the syntax styles on Linux systems. For example "PS ax-f".
But in this post we shall mostly focus on the UNIX style syntax.
Use two style things at the same time, such as PS ax-f but our article is mostly Unix-style
How to use PS command

1. Display All Processes

Show All Processes

The following command'll give a full list of processes

The following two commands show all the processes:

$ ps AX
$ ps-ef

Pipe the output to ' less ' to make it scrollable.

After using the pipe link less command, we can use the scroll bar to view specific information

Use the "U" option or "f" option to display detailed information about the processes

Use either u or-f to display details

$ ps aux
$ ps-ef-f

Why is the USER column does displaying my username, but showing others like root, www-data etc?

Why the user bar doesn't show my username, but it shows someone else's, for example, Root, Www-data

For all usernames (including yours) if the ' length is greater than 8 characters then PS would fall Instead of username.

For all users, if the string is >8, then PS will only display your UID

Why is the USER column does displaying my username, but showing others like root, www-data etc?
Why the user bar doesn't show my username, but it shows someone else's, for example, Root, Www-data
For all usernames (including yours) if the ' length is greater than 8 characters then PS would fall Instead of username.
For all users, if the string is >8, then PS will only display your UID

2. Display process by user

Show all processes for a user

To filter the processes by the owning user use the "-u" option followed by the username. Multiple usernames can be provided separated by a comma.

To display a single user-owned process-U "username", you can enter a username, and split by ","

$ 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 or process ID

Displays the process (or process ID) of the specified name

To search the processes by their name or command use the "-C" option followed by the search term.

We can find the process of a specific name through-C

$ ps-c Apache2

PID TTY Time CMD
2359? 00:00:00 apache2
4524? 00:00:00 apache2
4525? 00:00:00 apache2

...

To display processes by process ID, use the "-P" option and provides the process IDs separated by comma.

You can use the-P option to display specific ID large process information, or you can pass "," to pass multiple

$ ps-f-P 3150,7298,6544

The "-C" must is provided with the exact process name and it cannot actually search with a partial name or wildcard. To search the process list more flexibly, the usual grep command has to be used

The-c option requires that we enter an exact matching process name, so we prefer to use grep for filtering

$ PS-EF | grep Apache

4. Sort process by CPU or memory usage

Sort by CPU or memory

System administrators often want to find out processes that are consuming lots of memory or CPU. The sort option would sort the process list based on a particular field or parameter.

System administrators often need to find the culprit that consumes CPU or memory, sorting options allow us to sort by the specified parameters

Multiple fields can be specified with the ' –sort ' option separated by a comma. Additionally the fields can is prefixed with a "-" or "+" symbol indicating descending or ascending sort respectively. There are lots of parameters on which the process list can be sorted. Check the mans page for the complete list.

Multiple parameters We can use the –sort to sort, by "," to enter, we can sort the parameters in front of the home "-" or "+", indicating the sequence or reverse order, more options available through the man manual to find

$ ps aux--sort=-pcpu,+pmem

Display the top 5 processes consuming most of the CPU.

Shows 5 processes that consume the highest CPU

$ 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. Display process hierarchy in a tree style

To display processes in the form of a process tree

Many processes are actually forked out of the some parent process, and knowing this parent child relationship is often helpful . The ' –forest ' option would construct an ASCII art style tree view of the process hierarchy.

Many processes actually fork through many of the parent processes, and often know that the parent-child relationship is very much in use, –forest can achieve this

The following command would search for processes by the name apache2 and construct a and display detailed information.

The following command looks for the apache2 process and displays the information in tree form

$ 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 the ' sorting with the ' tree style display, as they both effect the ' order of display ' in different ways.

Note: You do not have to use the sort option when displaying information in tree form, because this can be messy

Try not to use the ' sorting with the ' tree style display, as they both effect the ' order of display ' in different ways.

Note: You do not have to use the sort option when displaying information in tree form, because this can be messy

6. Display child processes of a parent process

Show from Process:

This is a example of finding all forked Apache processes.

The following example is a child process that looks for all Apache:

$ 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

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

The first process is owned by root, so it's your main process, and then all the other apache2 processes are fork out of this process, and the following command shows all APACHE2 processes through the ID of the main process

$ 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. Display threads of a process

Show all threads under the process

The "-l" option would display the threads along with the processes. It can be used to display all threads the a particular process or all processes.

The-l option displays all threads that can be used to display threads under the specified process

The following command shall display the threads owned by the process with ID 3150.

The following command shows all threads under process 3150

$ ps-p 3150-l

8. Change the columns to display

Specify the options to display

The PS command can be configured-a selected list of columns only. There are a large number of columns to-show and the full list are available in the man pages.

PS command can specify the data bar we want to display, PS There are many columns can be displayed, the specific reference to the man manual

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

The following command will only show PID, Username,cpu,memory, and Comm

$ ps-e-O Pid,uname,pcpu,pmem,comm

It is possible to rename the column labels

We can also rename these columns.

$ 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. Display Elapsed time of processes

To show how long a process is running

The elapsed time indicates, how long the process has been running for. The column for elapsed. Shown by default, and has to being brought in using the "-o" option

By default, the running time of the process is not displayed by default, and if you want to view it, you must use the-o option

$ ps-e-O pid,comm,etime

Turn PS into an Realtime process viewer

Convert PS to real-time display of process Monitor

As usual, the watch command can be used to turn PS into a realtime process reporter. Simple example are like this

In general, we can use the Watch command to let our PS information display in real time, the simple example is as follows:

$ watch-n 1 ' ps-e-o pid,uname,cmd,pmem,pcpu--sort=-pmem,-pcpu | Head-15 '

The output results are 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 would is updated every 1 second to refresh the stats. However do does not have similar to top.

The output is updated every 1 time, and there is a difference between this and top.

You would notice this output of top/htop command changes much more frequently the compared to the PS command.

This is because the "top output sorts" "A" is a mix of CPU usage and memory usage. But the above PS command sorts in a simpler manner, taking 1 column at a time (like school maths). So it would isn't update rapidly like top.

You'll notice that the top/htop command has a much higher refresh rate than PS.

This is because the output of the top command is output based on a combined CPU and memory order, but the PS command above is a simple output, so it doesn't

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.