preface

At the mention of process management and view, most people will jump out of an instruction: ps – ef | grep process name, indeed, this is a very practical instruction, can view the process exists, you can also view the process of executable files, etc.

However, this time I’d like to take a closer look at the ps command as follows:

  • 1. What is ps command?
  • 2. What is the style of ps command?
  • 3. Common parameters of the ps command?

What is ps instruction

The full name of ps command is Process status, that is, process status. Compared with the TOP command, ps command displays the snapshot of the current process status instead of real-time update.

Ps command style

This is what I didn’t notice before, ps command parameters actually support three styles, namely UNIX, BSD and GNU, different styles, meaning the input of parameters in different forms.

  • 1. In THE UNIX style, the parameter must be preceded by a hyphen (-)
  • 2. In the BSD style, the parameter cannot start with a hyphen (-)
  • 3. If it is GNU style, it must be preceded by two hyphens (-)

The documentation for the ps command notes that in practice, you can mix these three styles freely, but inevitably, this mix can lead to certain conflicts, typically as follows:

#This form is BSD style, where the parameter u represents: display in user-based format
$ ps aux | grep nginxHongxinxie 94342 0.0 0.0 4345072 592?? Nginx: worker process hongxinxie 8611 0.0 0.0 4320056 600?? Ss 05 03 PM 05:00.03 nginx: Master process nginx
#Because of the added"-"In this case, the parameter u stands for user. The user name must be added to indicate the process that filters the specified user from all processes
$ ps -aux | grep nginx
ps: No user named 'x'

#Select the process whose user is root
$ ps -u root 
Copy the code

Common parameters of the ps command

The common parameters of the ps command are -a, -a, -e, -f, -x, -u, and -o

1. Whether it is -a, -a, or a, although there are some differences, it can be thought of as “show all processes” in most scenarios.

#The main difference between -a and -A is the presence or absence of session leaders.-a: All processes (UNIX style) -a: All with tty, except session leaders -A: All with tty, except session leaders All with tty, including other users (BSD style, showing tty related processes, basically equivalent to -a)Copy the code

2. The -e parameter is the same as the -a parameter, but the -e parameter is different from the e parameter

#Show all processes
$ ps -e
PID TTY          TIME CMD
1 pts/0    00:00:00 bash
12596 pts/1    00:00:00 man

#Displays the environment variables used by the command
$ ps e
PID TTY      STAT   TIME COMMAND
1   pts/0    Ss+    0:00 /bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=dea2e5f26346 TERM=xterm
Copy the code

3. Difference between -F and F:

#-f will output PID, TTY, TIME, CMD, TTY, TTY, TTY, TTY, TTY, etc
$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
root     12613     0  0 06:38 pts/3    00:00:00 bash
root     12660 12613  0 07:51 pts/3    00:00:00 ps -f

#The f parameter displays processes in a hierarchical format, indicating the parent-child relationship between processes
$ ps f
PID    TTY      STAT   TIME COMMAND
12613 pts/3    Ss     0:00 bash
12659 pts/3    R+     0:00  \_ ps f
12587 pts/1    Ss     0:00 bash
12596 pts/1    S+     0:00  \_ man ps
12608 pts/1    S+     0:00      \_ pager
12569 pts/2    Ss+    0:00 bash
Copy the code

The preceding fields are described as follows:

  • UID: The user who started these processes.
  • PID: indicates the ID of a process.
  • PPID: the process number of the parent process (if it was started by another process)
  • C: CPU usage during the process life cycle
  • STIME: indicates the system time when a process is started
  • TTY: Terminal device on which the process is started
  • TIME: indicates the total CPU TIME required by a running process
  • CMD: indicates the name of the program to start

4. The difference between -U and U (also mentioned in the previous style) :

#-u displays the processes of a specific user in UNIX style
$ ps -u root
PID   TTY          TIME CMD
  1   pts/0    00:00:00 bash
12569 pts/2    00:00:00 bash
12587 pts/1    00:00:00 bash

#U is BSD style, which means display in user-based format
$ ps uUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 18236 3160 PTS /0 Ss+ Jan15 0:00 /bin/bash root 12569 0.0 0.1 18244 3308 PTS /2 Ss+ Jan15 0:00 bash root 12587 0.0 0.1 18244 3204 PTS /1 Ss 06:36 0:00 bashCopy the code

The preceding fields are described as follows:

  • VSZ: the size of a process in memory, expressed in kilobytes
  • RSS: Physical memory occupied by a process when it is not paged out
  • STAT: indicates the two-character status code of the current process

5. Differences between -x and X:

#1. '-x' vs. 'x' When I tested it on Linux and Mac, there was no difference between the two

#2. When -x and x are used alone, they are basically the same as -a
$ ps x
PID TTY      STAT   TIME COMMAND
  1 pts/0    Ss+    0:00 /bin/bash
12569 pts/2    Ss+    0:00 bash
12587 pts/1    Ss     0:00 bash
12596 pts/1    S+     0:00 man ps
12608 pts/1    S+     0:00 pager
12613 pts/3    Ss     0:00 bash
12664 pts/3    R+     0:00 ps x

#3. When -x is paired with -a, it shows processes without controlling Terminal
$ ps -a
PID TTY          TIME CMD
12596 pts/1    00:00:00 man
12608 pts/1    00:00:00 pag

#3.1 with the -x parameter, part of the process is added
$ ps -ax
PID TTY      STAT   TIME COMMAND
  1 pts/0    Ss+    0:00 /bin/bash
12569 pts/2    Ss+    0:00 bash
12587 pts/1    Ss     0:00 bash
12596 pts/1    S+     0:00 man ps
12608 pts/1    S+     0:00 pager
12613 pts/3    Ss     0:00 bash
12669 pts/3    R+     0:00 ps -ax
Copy the code

6.-o parameter: This parameter specifies the format of the output, or which fields to output

$ ps -o uid,pid,%cpu,%mem
UID   PID %CPU %MEM
 0  12613  0.0  0.1
 0  12671  0.0  0.0
Copy the code

The parameters you can use are:

#Here are some common ones, others can be found through man Ps
%cpu       percentage CPU usage (alias pcpu)
%mem       percentage memory usage (alias pmem)
comm       command
command    command and arguments
cpu        short-term CPU usage factor (for scheduling)
etime      elapsed running time
gid        processes group id (alias group)
pgid       process group number
pid        process ID
ppid       parent process ID
state      symbolic process state (alias stat)
tdev       control terminal device number
time       accumulated CPU time, user + system (alias cputime)
tty        full name of control terminal
uid        effective user ID
user       user name (from UID)
utime      user CPU time (alias putime)
vsz        virtual size in Kbytes (alias vsize)
xstat      exit or stop status (valid only for stopped or zombie process)
...
Copy the code

7. Ps – EF and PS AUX comparison:

The only difference is that the output parameters are slightly different. You can try it yourself.

In addition, you may find that if you add grep command, the header information will be lost, because it is filtered out. If you want to display it, you can refer to here: grep command to keep the first line

supplement

What are session leaders

As we know, in Linux, each process has several ids associated with it: PID, PPID, PGID, and SID.

  • PID: indicates the Process ID, which is the unique identifier of a Process. When a Process exits, its PID is released.
  • PPID: Parent Process ID: indicates the Process ID of the Parent Process
  • PGID: INDICATES the ID of the Process Group, that is, the PID of the Leader. If the PID of a Process is == PGID, it indicates that the Process is the Leader
  • SID: Indicates the Session ID, that is, the PID of the Session Leader. If the PID of a process == SID, it means that it is the Session Leader

1. Session and process group are just a way to identify multiple processes as a unit. All members of a process group always belong to the same session, but a session may have multiple process groups

2. Typically, a shell program (bash, or sh) will be the session leader, and each pipe executed by the shell will be a process group, to make it easy for the shell to kill its child processes upon exit.

Ps-a and PS-A can be obtained:

ps -Ao pid,ppid,pgid,sid,comm
  PID  PPID  PGID   SID COMMAND
    1     0     1     1 bash
12569     0 12569 12569 bash
12587     0 12587 12587 bash
12596 12587 12596 12587 man
12608 12596 12596 12587 pager
12613     0 12613 12613 bash
12654 12613 12654 12613 ps

#The -a command will exclude the session leader, that is, the bash process will not be displayed
$ ps -ao pid,ppid,pgid,sid,comm

  PID  PPID  PGID   SID COMMAND
12596 12587 12596 12587 man
12608 12596 12596 12587 pager
12653 12613 12653 12613 ps
Copy the code

conclusion

This is the end of the ps command introduction, if you want to see more about the meaning of the ps command, you can use the man PS command, if you do not want to read the English, you can also read here: ps under the three styles of Linux (Unix, BSD, GNU) parameter description

If you want to see more use of the ps command, here are 10 important Linux PS commands in action

Write in the last

Dear bosses, creation is not easy, but it needs to be honed and summarized constantly, welcome to pay attention to me, I am Yan Gan, I will share with you all kinds of practical programming knowledge and skills, your praise and attention is the biggest motivation for my progress and creation!!