10 Important Linux PS command combat

Source: Internet
Author: User
Tags cpu usage

Linux is a UNIX-derived operating system, and Linux has a tool PS to view the current process. This tool can be used on the command line.

What is the PS command?

View its man manual to see that the PS command gives a snapshot of the process in the current system. It can capture the process state of a system in an event. You can use the top command if you want to constantly update the status of the view.

PS command supports three types of syntax formats used

    1. UNIX style, options can be grouped together, and the option must have "-" hyphens before
    2. BSD style, options can be grouped together, but cannot have a "-" hyphen before the option
    3. GNU style long option with two "-" hyphens in front of options

We can mix these styles, but there may be conflicts. This article uses the UNIX-style PS command. Here are examples of the use of more PS commands in daily life.

1. Execute PS command without parameters

This is a basic PS to use. Execute this command from the console and view the results.

The result is 4 columns of information displayed by default.

    • PID: The process number of the command (CMD) running
    • TTY: Where the command is running (terminal)
    • Time: CPU processing times consumed by the command that is running
    • CMD: The command that the process runs

This information is not sorted when it is displayed.

2. Show all current processes

Use the-a parameter. -A represents all. Plus the x parameter shows the process without the control terminal.

$ ps-ax

The result of this command may be very long. For ease of viewing, it can be used in conjunction with less commands and pipelines.

$ Ps-ax | Less

3. Based on user filtering process

We can use the-u parameter when we need to see a particular user process. For example, we want to see the user ' Pungki ' process, you can use the following command:

$ ps-u Pungki

4. Filtering processes through CPU and memory usage

You might want to filter the results by CPU or memory usage, so you can find which process is taking up your resources. To do this, we can use the AUX parameter to display comprehensive information:

$ Ps-aux | Less

When the results are long, we can use the pipe and less command to filter.

The default result set is not well-ordered. Can be sorted by –sort command.

Sort Ascending according to CPU usage

$ Ps-aux--sort-pcpu | Less

Sort Ascending According to memory usage

$ Ps-aux--sort-pmem | Less

We can also merge them into one command and display the top 10 results by pipeline :

$ ps-aux--sort-pcpu,+pmem | head-n 10
5. Filter by process name and PID

Use the-c parameter, followed by the name of the process you are looking for. For example, to display information about a process called Getty, you can use the following command:

$ ps-c Getty

If you want to see more details, we can use the-f parameter to see a list of formatted information:

$ ps-f-C Getty

6. Filter processes based on threads

If we want to know the thread of a particular process, we can use the-l parameter followed by a specific PID.

$ ps-l 1213

7. Tree-shaped display process

Sometimes we want to show the process in a tree structure, and you can use the-AXJF parameter.

$ps-AXJF

Or you can use another command.

$ pstree

8. Display of security information

If you want to see who is now logged into your server . You can use the PS command to add related parameters:

$ Ps-eo Pid,user,args

Parameter-e Displays all process information, and the-o parameter controls the output. The Pid,user and args parameters show the PID, the user who runs the app, and the app.

The keywords that can be used with the-e parameter are args, cmd, comm, command, fname, Ucmd, Ucomm, Lstart, Bsdstart, and start.

9. Format the output of the process created by the root user (real or valid UID)

When a system administrator wants to see the process run by the root user and other relevant information about the process, the following command can be used:

$ ps-u root-u Root U

The-u parameter filters the process by real user ID (RUID), which selects the real user name or ID from the list of users. The real user is the user who actually created the process.

The-u parameter is used to filter valid user IDs (Euid).

The final u parameter is used to determine the output of the user-specific format, consisting of user, PID,%cpu,%MEM, VSZ, RSS, TTY, STAT, START, time, and command.

Here is the output of the above command:

10. Monitor process status in real time using PS

The PS command shows the current process state of your system, but the result is static.

When there is a situation, we need to filter the process by CPU and memory usage as mentioned in the 4th above , and we want the result to be refreshed every second. To do this, we can combine the PS command with the Watch command.

$ watch-n 1 ' ps-aux--sort-pmem,-pcpu '

If the output is too long, we can also limit it, such as the first 20, we can use the head command to do it.

$ watch-n 1 ' ps-aux--sort-pmem,-PCPU | Head 20 '

The dynamic view here is not the same as the top or htop command. But the advantage of using PS is that you can define the fields that are displayed, and you can select the fields you want to see.

For example, if you only need to look at the information named ' Pungki ' user, you can use the following command:

$ watch-n 1 ' ps-aux-u pungki U--sort-pmem,-PCPU | head 20 '

Conclusion

You may use the PS command every day to monitor your Linux system. But in fact, you can use the parameters of the PS command to generate a variety of reports you need.

Another advantage of the PS command is that PS is installed by default on a variety of Linux systems, so you can just use it.

Don't forget to view more parameters with man Ps. (LCTT: Because the PS command is ancient and important, it has different parameters in various UNIX, BSD, Linux and other systems, so if you are not using a Linux system, consult your documentation for specific available parameters.) )

10 Important Linux PS command combat

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.