10 important Linux ps commands are actually

  

Linux is a derivative of Unix operating system
, Linux has built-in tools to view the current process ps. This tool can be used on the command line.


What is the PS command?

Check its man page to see that the ps command gives a snapshot of the process in the current system. It captures the state of the system in a certain event. If you want to keep updating this state of view, you can use the top command.

The ps command supports three syntax formats used

  1. UNIX style, options can be combined, and must have a "-” hyphen before the option
  2. BSD style, options can be combined, but there can be no <quo;-”hyphen
  3. GNU-style long option before the option, there are two "-" before the option "-” hyphen
    < Br>

    We can mix these styles, but there may be conflicts. This article uses the UNIX-style ps command. Here are examples of ps commands that are used more often in everyday life.
    1. Execute the ps command without arguments

    This is a basic ps use. Execute this command in the console and see the results.


    The results will display 4 columns of information by default.

  4. PID: Process number of the running command (CMD)
  5. TTY: The location where the command is run (terminal)
  6. TIME: The command that is running CPU processing time occupied
  7. CMD: Commands run by this process

    These information is not sorted when displayed.
    2. Show all current processes

    Use the -a parameter. -a stands for all. Adding the x parameter at the same time will show 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 the less command and pipeline.
    $ ps -ax |  Less


    3. According to the user filtering process

    We can use the -u parameter if we need to view a specific user process. For example, if we want to view the user & rsquo;pungki’ process, you can use the following command:
    $ ps -u pungki


    4. Filter the process through cpu and memory usage

    Maybe you I 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 result is very long, we can use the pipe and the less command to filter.

    The default result set is unordered. Can be sorted by the –sort command.

    Sort in ascending order according to CPU usage
    $ ps -aux --sort -pcpu |  Less


    Sorting in ascending order based on memory usage
    $ ps -aux --sort -pmem |  Less


    We can also merge them into one command and display the top 10 results through the pipeline:
    $ ps -aux --sort -pcpu,+pmem |  Head -n 105. Filter by process name and PID

    Use the -C parameter followed by the name of the process you are looking for. For example, if you want to display information about a process named getty, you can use the following command:
    $ ps -C getty


    If you want to see more details, we can use - f parameter to view a list of formatted information:
    $ ps -f -C getty


    6. Filter the process according to the thread

    If we want to know the thread of a particular process, you can use The -L parameter, followed by a specific PID.
    $ ps -L 1213


    7. Tree Display Process

    Sometimes we want to display the process in a tree structure, you can use the -axjf parameter.
    $ps -axjf


    Or you can use another command.
    $pstree


    8. Displaying Security Information

    If you want to see who is currently logged into your server. You can use the ps command with the relevant 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 running the application and the application.


    The keywords that can be used with the -e parameter are args, cmd, comm, command, fname, ucmd, ucomm, lstart, bsdstart, and start.
    9. Formatting the process created by the root user (real or valid UID)

    When the system administrator wants to view the process run by the root user and other related information about this process, The following command:
    $ ps -U root -u root u

    -U The parameter filters the process by real user ID (RUID), which selects the real username 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 the valid user ID (EUID).

    The final u parameter is used to determine the output in the format for the user. It consists of the User, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME and COMMAND columns.

    There are output results of the above command:


    10. Using PS to monitor process status in real time

    ps command will display the current process status of your system, but This result is static.

    When there is a situation, we need to filter the process by CPU and memory usage as mentioned in the fourth point 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 is done.
    $ watch -n 1 ‘ps -aux --sort -pmem, -pcpu |  Head 20’


    The dynamic view here is not like 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 view.

    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 to monitor your Linux system every day. But in fact, you can generate various reports you need by using the parameters of the ps command.

    Another advantage of the ps command is that ps is installed by default on all Linux systems, so you only need to use it.

    Don't forget to see more parameters by man ps. (LCTT: Because the ps command is old and important, it has different parameters on different UNIX, BSD, Linux, etc., so if you are not using a Linux system, please consult your documentation for specific parameters. )

Copyright © Windows knowledge All Rights Reserved