1. What is Ps?

To monitor and control a process, you must first know the status of the current process, that is, you need to view the current process. The ps command is the most basic process view command. You can use this command to determine which processes are running and their running status, whether the process is terminated, whether the process is zombie, which processes occupy too many resources, and so on. Most of the information can be obtained by executing this command.

Ps is to display the state of the instant process, not dynamic continuous; If you want to monitor the process in real time, use the top command.

Basic parameters:

  • -a: All processes are displayed and have the same utility as -e;
  • -a: displays all processes under the current terminal, including those of other users.
  • -u: indicates the user-dominated process status.
  • X: Usually used with the parameter a to list more complete information.

Output format planning:

  • L: List the information of this PID in a longer and more detailed way;
  • J: Jobs format
  • -f: makes a more complete output.

Let’s put a command into practice to see how different parameters work.


2. What is the output of the ps command without parameters?

This is a basic ps use, let’s take a look at executing this command in the console and see the results.

The result displays four columns of information by default:

  • PID: indicates the process id of the running command (CMD)
  • TTY: Where the command is run (terminal)
  • TIME: indicates the CPU processing TIME occupied by running the command
  • CMD: the command that the process runs

This information is not sorted when displayed.


3. How do I display all current processes?

Use the -a parameter, where -a represents all. Adding an x parameter to the process shows that there is no control terminal.

$ ps -ax
Copy the code

The result of this command may be very long. You can use it in combination with the less command and pipes for easy viewing.

$ ps -ax | less
Copy the code


4. How to filter information according to the users of the process?

We can use the -u argument in cases where we need to view a specific user process. For example, if we want to check the progress of user ‘pungki’, we can use the following command:

$ ps -u pungki
Copy the code


5. How do I filter processes by CPU and memory usage?

Perhaps you want to filter the results by CPU or memory usage so that you can find which processes are using your resources. To do this, we can use the AUX parameter to display comprehensive information:

$ ps -aux | less
Copy the code

The default result set is unsorted. This can be done with the –sort command.

5.1 Sort the CPU usage in ascending order

$ ps -aux --sort -pcpu | less
Copy the code

5.2 Sort memory usage in ascending order

$ ps -aux --sort -pmem | less
Copy the code

5.3 We can also merge them into a single command and pipe the first 10 results:

$ ps -aux --sort -pcpu,+pmem | head -n 10
Copy the code

6. How to filter by process name and PID?

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

$ ps -C getty
Copy the code

If we want to see more detail, we can use the -f argument to view the formatted list of information:

$ ps -f -C getty
Copy the code


7. How do you filter processes by thread?

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

$ ps -L 1213
Copy the code


8. How to display the process in tree form?

Sometimes we want to display processes in a tree structure, using the -axjf argument.

$ ps -axjf
Copy the code

Or you can use another command.

$ pstree
Copy the code


9. How do I display safety information?

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

$ ps -eo pid,user,args
Copy the code

The -e parameter displays information about all processes, and the -o parameter controls output. The Pid,User, and Args parameters show the Pid, the User running the application, and the application.

-e
Args, CMD, Comm, command, fname, UCMD, uCOMM, lstart, bsdstart and start


10. How to format the output of processes created by root (real or valid UID)?

The system administrator can run the following command to view the process run by user root and other information about the process:

$ ps -U root -u root u
Copy the code

The -u parameter filters processes by real user ID(RUID). It selects the real user name or ID from the user list. The real user is the user who actually creates the process.

The -u parameter is used to filter valid user ids (EUids).

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

Here is the output of the above command:


11. How do I use PS to monitor process status in real time?

The ps command displays the current state of your system’s processes, but the result is static.

When we have a situation where we need to filter processes by CPU and memory usage as mentioned in point 4 above, and we want the results to refresh every second. To do this, we can combine the ps command with the watch command.

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

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

$watch - n 'ps - aux 1 - a sort - pmem, - pcpu | head 20'Copy the code

Dynamic viewing here is not like the top or htop commands. But the nice thing about using Photoshop is that you can define the fields that are displayed, and you can select the fields that you want to see.

For example, if you only need to see information about the user named ‘pungki’, you can use the following command:

$watch - n 1 'ps - aux -u pungki U - sort - pmem, - pcpu | head 20'Copy the code

12. The final

You probably 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 any report you need.

Another advantage of the ps command is that ps is installed by default on every Linux system, so you just need to use it. Don’t forget to check out man Ps for more parameters.