[Linux] Detailed explanation of ps command, view process pid

1. ps overview

The ps command is the most commonly used command to monitor processes. Through this command, you can view detailed information about all running processes in the system.

There are many different ways to use the ps command, which often confuses beginners. In various Linux forums, posts asking about the syntax of the ps command are common. Such situations are also attributed to the long history and huge factions of UNIX. The syntax of the ps command is different on different Linux distributions. To this end, Linux has adopted a compromise approach, which is to integrate various styles and take into account those users who are accustomed to using the ps command on other systems.

2. ps syntax

The basic format of the ps command is as follows:

#查看系统中所有的进程,使用 BS 操作系统格式
[root@localhost ~]# ps aux

#查看系统中所有的进程,使用 Linux 标准命令格式
[root@localhost ~]# ps -les

Options:

  • a: Display all processes of a terminal, except session leads;
  • u: Displays the user belonging to the process and memory usage;
  • x: Display processes without a controlling terminal;
  • -l: Long format displays more detailed information;
  • -e: display all processes;
  • -f displays in full format

As you can see, the ps command is a little different. Some of its options cannot be added with "-", such as the command "ps aux", where "aux" is an option, but "-" cannot be added in front of it.

If you execute the "man ps" command, you will find that in order to adapt to different UNIX-like systems, the help of the ps command has many available formats, which is inconvenient to remember. So, I recommend it to everyone 记忆几个固定选项即可. for example:

  • "ps aux" can view all processes in the system;
  • "ps -le" can view all processes in the system, and you can also see the PID and process priority of the process's parent process;
  • "ps -ef" is similar to le
  • "ps -l" can only see the processes generated by the current shell;

These three commands are enough. Let’s check them out separately below.

[Example 1] ps aux

[root@localhost ~]# ps aux
#查看系统中所有的进程
[root@EMS3 ~]# ps aux|head -n  10
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0 191932  5000 ?        Ss    2022  46:32 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S     2022   0:04 [kthreadd]
root         3  0.0  0.0      0     0 ?        S     2022   4:19 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S     2022   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S     2022   0:48 [migration/0]
root        10  0.0  0.0      0     0 ?        S<    2022   0:00 [lru-add-drain]
root        11  0.0  0.0      0     0 ?        S     2022   0:15 [watchdog/0]
…省略部分输出…

Or use ps aux |head -n 10 to display only the first 10 records to facilitate viewing the meaning of the column headers.

Insert image description here

[Example 2] ps -ef

[root@EMS3 ~]# ps -ef|head -n  10
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0  2022 ?        00:46:32 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2     0  0  2022 ?        00:00:04 [kthreadd]
root         3     2  0  2022 ?        00:04:19 [ksoftirqd/0]
root         5     2  0  2022 ?        00:00:00 [kworker/0:0H]
root         7     2  0  2022 ?        00:00:48 [migration/0]
root        10     2  0  2022 ?        00:00:00 [lru-add-drain]
root        11     2  0  2022 ?        00:00:15 [watchdog/0]

[Example 3] ps -le
The "ps aux" command can see all the processes in the system, and the "ps -le" command can also see all the processes in the system. Due to the "-l" option, the "ps -le" command can see more detailed information, such as the PID and priority of the parent process. But the basic functions of these two commands are the same, and mastering one of them is enough.

[root@localhost ~]# ps -le
[root@EMS3 ~]# ps -le|head -n  10
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0     1     0  0  80   0 - 47983 ep_pol ?        00:46:32 systemd
1 S     0     2     0  0  80   0 -     0 kthrea ?        00:00:04 kthreadd
1 S     0     3     2  0  80   0 -     0 smpboo ?        00:04:19 ksoftirqd/0
5 S     0     5     2  0  80   0 -     0 worker ?        00:00:00 kworker/0:0H
1 S     0     7     2  0 -40   - -     0 smpboo ?        00:00:48 migration/0
1 S     0     8     2  0  80   0 -     0 rcu_gp ?        00:00:00 rcu_bh
1 S     0     9     2  0  80   0 -     0 rcu_gp ?        00:58:12 rcu_sched
5 S     0    10     2  0  60 -20 -     0 rescue ?        00:00:00 lru-add-drain
5 S     0    11     2  0 -40   - -     0 smpboo ?        00:00:15 watchdog/0
…省略部分输出…

Or use to ps -ef |head -n 10display only the first 10 records to facilitate viewing the meaning of column headers
Insert image description here
[Example 4] If you don’t want to see all processes and just want to see which processes are generated by the current login, then just use the "ps -l" command. Got:

[root@localhost ~]# ps -l
#查看当前登录产生的进程
F S UID   PID  PPID C PRI NI ADDR SZ WCHAN TTY       TIME CMD
4 S 0   18618 18614 0  80  0 - 1681  -     pts/1 00:00:00 bash
4 R 0   18683 18618 4  80  0 - 1619  -     pts/1 00:00:00 ps

As you can see, logging in from the pts/1 virtual terminal this time only generated two processes: one is the Shell generated after logging in, which is bash; the other is the ps command being executed.

Let's talk about zombie processes again. The zombie process is usually caused by abnormal stop of the process or programming error, which causes the child process to end before the parent process, and the parent process does not correctly recycle the child process, resulting in the child process always existing in the memory. This is a zombie process. .

Zombie processes will have an impact on the stability of the host. Therefore, after a zombie process is generated, the software that generates the zombie process must be optimized to avoid generating zombie processes all the time. For zombie processes that have already been generated, they can be forcibly terminated after being found. .

3. Specify pid to view

Format:
ps -p xxx
ps -p xxx -f more detailed information

4. View the process startup path

#查看tomcat进程
ps -ef | grep tomcat

After a single process is started, there will be a path corresponding to the pid under /proc.
For example, if the PID is 1521 /proc/1521, ls -lyou will see information similar to the following:

-r--r--r--    1 oracle   oinstall        0 12月 29 18:58 cmdline
lrwxrwxrwx    1 oracle   oinstall        0 12月 29 18:58 cwd -> /XXX/ultserver_aa
-r--------    1 oracle   oinstall        0 12月 29 18:58 environ

cwd is the path of the process to be found

Guess you like

Origin blog.csdn.net/m0_45406092/article/details/130661189