As a Linux system developer, have you ever encountered problems such as process freezes and unresponsive applications, but you don’t know how to effectively analyze and handle them? If that's the case, then you've come to the right place! This article will introduce process analysis technology under Linux to help you gain a deeper understanding of the mechanism of process operation and effectively discover and solve problems.
1. What is ps?
To monitor and control the process, you must first understand the situation of the current process, that is, you need to view the current process. The ps command is the most basic process viewing command. Use this command to determine which processes are running and their running status, whether the process has ended, whether there are zombies in the process, which processes occupy too many resources, etc. In short, most of the information can be obtained by executing this command.
ps displays the status of the instantaneous process and is not dynamic and continuous; if you want to monitor the process in real time, you should use the top command.
Basic parameters:
-A: All processes are displayed, which has the same effect as -e;
-a: Display all processes under the current terminal, including processes of other users;
-u: User-oriented process status;
x: Usually used together with the a parameter to list more complete information.
Output format planning:
l: A longer and more detailed list of the PID information;
j :jobs format
-f: Make a more complete output.
Let’s practice a command below to see what effects different parameters have.
2. What will be output when executing ps command without parameters?
This is a basic ps usage, let’s take a look at executing this command in the console and view the results.
The results will display 4 columns of information by default:
PID: Process number of the running command (CMD)
TTY: The location (terminal) where the command is run
TIME: CPU processing time occupied by the running command
CMD: The command run by this process
The information is displayed unsorted.
3. How to display all current processes?
Use the -a parameter, -a represents all. At the same time, adding the x parameter will display processes that do not control the terminal.
$ ps -ax # 这个命令的结果或许会很长。为了便于查看,可以结合less命令和管道来使用。 $ ps -ax | less
4. How to filter information according to the user of the process?
When we need to view a specific user process, we can use the -u parameter. For example, if we want to view the process of user 'pungki', we can use the following command:
$ ps -u pungki
5. How to filter processes by cpu and memory usage?
Maybe you 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 very long, we can use pipes and less commands to filter.
The default result set is not sorted. Sorting can be done with the –sort command.
5.1 Sort in ascending order according to CPU usage
$ ps -aux --sort -pcpu | less
5.2 Sort in ascending order according to memory usage
$ ps -aux --sort -pmem | less
5.3 We can also combine them into one command and pipe the top 10 results:
$ ps -aux --sort -pcpu,+pmem | head -n 10
6. How to 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 the -f parameter to view the formatted information list:
$ ps -f -C getty
7. How to filter processes based on threads?
If we want to know the threads of a specific process, we can use the -L parameter, followed by a specific PID.
$ ps -L 1213
8. How to display the process in tree shape?
Sometimes we want to display the process in a tree structure, we can use the -axjf parameter.
$ ps -axjf
或者可以使用另一个命令。
$ pstree
9. 如何显示安全信息?
如果想要查看现在有谁登入了你的服务器。可以使用ps命令加上相关参数:
$ ps -eo pid,user,args
参数 -e 显示所有进程信息,-o 参数控制输出。Pid,User 和 Args参数显示PID,运行应用的用户和该应用。
能够与 -e 参数 一起使用的关键字是args, cmd, comm, command, fname, ucmd, ucomm, lstart, bsdstart 和 start。
10. 如何格式化输出root用户(真实的或有效的UID)创建的进程?
系统管理员想要查看由root用户运行的进程和这个进程的其他相关信息时,可以通过下面的命令:
$ ps -U root -u root u
-U 参数按真实用户ID(RUID)筛选进程,它会从用户列表中选择真实用户名或 ID。真实用户即实际创建该进程的用户。
-u 参数用来筛选有效用户ID(EUID)。
最后的 u 参数用来决定以针对用户的格式输出,由User, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME 和 COMMAND这几列组成。
这里有上面的命令的输出结果:
11. 如何使用PS实时监控进程状态?
ps 命令会显示你系统当前的进程状态,但是这个结果是静态的。
当有一种情况,我们需要像上面第四点中提到的通过CPU和内存的使用率来筛选进程,并且我们希望结果能够每秒刷新一次。为此,我们可以将ps命令和watch命令结合起来。
$ watch -n 1 ‘ps -aux --sort -pmem, -pcpu’
如果输出太长,我们也可以限制它,比如前20条,我们可以使用 head 命令来做到。
$ watch -n 1 ‘ps -aux --sort -pmem, -pcpu | head 20’
这里的动态查看并不像top或者htop命令一样。但是使用ps的好处是你能够定义显示的字段,你能够选择你想查看的字段。
举个例子,如果你只需要看名为’pungki’用户的信息,你可以使用下面的命令:
$ watch -n 1 ‘ps -aux -U pungki u --sort -pmem, -pcpu | head 20’
12. 最后
你也许每天都会使用ps命令来监控你的Linux系统。但是事实上,你可以通过ps命令的参数来生成各种你需要的报表。
ps命令的另一个优势是ps是各种 Linux系统都默认安装的,因此你只要用就行了。不要忘了通过 man ps来查看更多的参数。
通过本文,我们已经学习了Linux下的进程分析技术,理解了进程的运行机制和调度方法,并且掌握了一些常见的进程分析工具和技巧。进程是Linux系统中非常重要的概念和组成部分,良好的进程分析技术可以帮助我们快速定位和解决问题,提高系统的运行效率和稳定性。希望本文能够对你有所启发和帮助,让你更加深入地了解进程分析领域,在职业发展中更上一层楼!
The above is the detailed content of In-depth exploration of process analysis technology under Linux!. For more information, please follow other related articles on the PHP Chinese website!