The ps command in Linux is short for Process Status. Using the ps command, you can list the processes that are currently running in the system. The ps command lists snapshots of processes at the moment the ps command is executed. To dynamically display process information, use the top command.

To monitor and control a process, you must first understand the current process, that is, you need to view the current process, and the ps command is the most basic and very powerful process view command. Use this command to determine which processes are running and in what state they are running, whether they are terminated, whether they are dead, which processes are taking up too many resources, and so on. In short, most of the information can be obtained by executing this command.

Ps provides us with a one-time view of the process, which is not dynamically continuous; If you want to monitor process time, you should use the top tool.

The kill command is used to kill a process.

Processes in Linux have five states

  1. Run (running or waiting in a run queue)
  2. Interrupt (in hibernation, blocked, waiting for a condition to develop or for a signal to be received)
  3. Noninterruptible (no wake up and no run upon receipt of a signal, the process must wait until an interrupt occurs)
  4. Dead (process terminates, but process descriptor exists until released by parent wait4() system call)
  5. Stop (process stops running after receiving SIGSTOP, SIGSTP, SIGTIN, SIGTOU signal)

The PS tool identifies five status codes of a process

  • D uninterruptible sleep (usually IO)
  • R Run runnable (on run queue)
  • S interrupt sleeping
  • T traced or stopped
  • Z zombie a defunct (” zombie “) process

Command syntax

Ps (parameters)

The command parameter

  • -a: Displays all the programs executed on all terminals except the stage job leader.
  • A: Displays all programs under the current terminal, including other users’ programs.
  • -a: Displays all programs.
  • -c: displays the CLS and PRI fields.
  • C: When listing programs, show the actual instruction name of each program, without indicating paths, options, or resident services.
  • -C< instruction name > : specifies the name of the instruction to be executed and lists the status of the program for the instruction.
  • -D: Displays all programs, but not those of the stage job leader.
  • -e: This option has the same effect as specifying the “A” option.
  • E: When programs are listed, display the environment variables used by each program.
  • -f: Displays the UID,PPIP,C, and STIME fields.
  • F: Displays a tree structure with ASCII characters to express the relationships between programs.
  • -g< Group name > : This option has the same effect as specifying the “-g” option, but can also be specified using the name of the stage job leader.
  • G: Displays all programs under the current terminal, including those of the group leader.
  • -g < Group id > : Lists the status of the programs that belong to the group, which can also be specified using the group name.
  • H: The title column is not displayed.
  • -h: displays the tree structure, indicating the relationship between programs.
  • -j or J: Displays program status in a work-controlled format.
  • -L or L: Displays program status in a detailed format.
  • L: Column information about the field.
  • -m or m: Displays all threads.
  • N: The USER and WCHAN fields are represented by numbers.
  • -n: Displays all programs except those under the PS command terminal.
  • -P < program identifier > : Specifies the program identifier and lists the status of the program.
  • P < Program identifier > : This option has the same effect as specifying the “-p” option, with a slight difference in the list format.
  • R: Lists only the programs that are currently being executed on the terminal.
  • -s< stage job > : Specifies the program identifier for a stage job and lists the status of the programs that belong to that stage job.
  • S: The program status is displayed in the format of program signal.
  • S: When a program is listed, it includes information about interrupted subroutines.
  • -t< Terminal number > : Specifies a terminal number and lists the status of programs belonging to that terminal.
  • T < terminal number > : This option has the same effect as specifying the “-t” option, with a slight difference in the list format.
  • -t: displays all programs under the current terminal.
  • -u< user id > : This option has the same effect as specifying the “-u” option.
  • U: Displays program status in a user-oriented format.
  • -u < User id > : Lists the status of the program belonging to the user, which can also be specified using the user name.
  • U< User name > : lists the status of the program belonging to that user.
  • V: Displays the program status in a virtual memory format.
  • -v or V: displays the version information.
  • -w or W: Displays program status in a broad format.
  • X: Displays all programs, not differentiated by terminal.
  • X: Use the old Linux I386 login format to display the program status.
  • -y: When used together with “-l”, the F(flag) field is not displayed and the RSS field is used instead of the ADDR field.
  • -< Program identifier > : This option has the same effect as specifying the “p” option.
  • –cols< characters per column > : sets the maximum number of characters per column.
  • –columns< columns > : This option has the same effect as specifying “–cols”.
  • –cumulative: This option has the same effect as the “S” option.
  • — desELECT: This option has the same effect as specifying the “-n” option.
  • –forest: This option has the same effect as specifying the “f” option.
  • –headers: Displays the header column repeatedly.
  • –help: Online help.
  • –info: Displays troubleshooting information.
  • –lines< Display columns > : Sets the number of columns to display.
  • –no-headers: This option has the same effect as the “h” option, except for the list format.
  • –group< group name > : This has the same effect as specifying the “-g” option.
  • –Group< Group identifier > : This option has the same effect as specifying the “-g” option.
  • — PID < program identifier > : This option has the same effect as specifying the “-p” option.
  • –rows< Number of columns to display > : This option has the same effect as specifying the “–lines” option.
  • — SID < stage job > : This option has the same effect as specifying the “-s” option.
  • –tty< terminal number > : This option has the same effect as specifying the “-t” option.
  • –user< user name > : This option has the same effect as specifying the “-u” option.
  • –User< User id > : This option has the same effect as specifying the “-u” option.
  • –version: This option has the same effect as specifying “-v”.
  • –widty< number of characters per column > : This option has the same effect as specifying “-cols”.

Display information about all processes

> ps -A
  PID TTY          TIME CMD
    1 ?        00:11:10 systemd
    2 ?        00:00:04 kthreadd
    4 ?        00:00:00 kworker/0:0H
    6 ?        00:00:07 ksoftirqd/0
    7 ?        00:00:02 migration/0
    8 ?        00:00:00 rcu_bh
    9 ?        00:43:24 rcu_sched
   10 ?        00:00:00 lru-add-drain
   11 ?        00:00:12 watchdog/0
   12 ?        00:00:10 watchdog/1
   13 ?        00:00:11 migration/1
   14 ?        00:06:22 ksoftirqd/1
   16 ?        00:00:00 kworker/1:0H
   17 ?        00:00:11 watchdog/2
   ...
Copy the code

Displays the threads started by the specified user

> ps -u deploy
  PID TTY          TIME CMD
 7698 ?        00:29:50 java
13783 ?        00:16:42 java
30027 ?        00:03:20 java
Copy the code

All process information and command lines are displayed

> ps -ef 
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jan08 ?        00:11:10 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2     0  0 Jan08 ?        00:00:04 [kthreadd]
root         4     2  0 Jan08 ?        00:00:00 [kworker/0:0H]
root         6     2  0 Jan08 ?        00:00:07 [ksoftirqd/0]
root         7     2  0 Jan08 ?        00:00:02 [migration/0]
Copy the code

Ps and grep are commonly used together to find a specific process

> ps -ef | grep java
ps -ef | grep ssh
root      1394     1  0 Jan08 ?        00:04:23 /usr/sbin/sshd -D
root     30899  1394  0 22:44 ?        00:00:00 sshd: root@pts/2
root     30922 30899  0 22:44 ?        00:00:00 /usr/libexec/openssh/sftp-server
root     31625  1394  0 22:49 ?        00:00:00 sshd: root [priv]
sshd     31627 31625  0 22:49 ?        00:00:00 sshd: root [net]
root     31804 30905  0 22:50 pts/2    00:00:00 grep --color=auto ssh
Copy the code

List the PID and related information that currently belongs to you for this login

> ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 R     0  1275 30905  0  80   0 - 38304 -      pts/2    00:00:00 ps
4 S     0 30905 30899  0  80   0 - 28895 do_wai pts/2    00:00:00 bash
Copy the code
  • F represents the flag of the program, and 4 represents the super user
  • S stands for the state of the program (STAT). The meaning of each STAT is described in this article
  • The UID program is owned by the UID
  • PID is the ID of this program!
  • PPID is the ID of its parent program
  • C Percentage of CPU usage
  • PRI is short for Priority. More on this later
  • NI is the Nice value, and we’ll talk about that in the next section
  • ADDR is the kernel function, which specifies which part of memory the program is in. If it’s a running program, it’s usually “-“
  • SZ Memory size used
  • WCHAN is the program currently in operation? – indicates that it is in operation
  • The terminal location of a TTY logon
  • TIME Indicates the CPU TIME used.
  • What are the commands given by CMD

Lists a program display similar to a program tree

> ps -axjf
    1  1394  1394  1394 ?           -1 Ss       0   4:23 /usr/sbin/sshd -D
 1394 30899 30899 30899 ?           -1 Ss       0   0:00  \_ sshd: root@pts/2
30905  1873  1872 30905 pts/2     1872 S+       0   0:00      |   \_ grep --color=auto ssh
30899 30922 30922 30922 ?           -1 Ss       0   0:00      \_ /usr/libexec/openssh/sftp-server
Copy the code

View process information in pages

> ps aux | more
Copy the code

An explanation of each column

> ps aux | more
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

Copy the code
  • USER USER of the process;
  • PID Indicates the ID of a process.
  • %CPU Indicates the CPU percentage used by processes.
  • %MEM Indicates the percentage of memory occupied.
  • VSZ The amount of virtual memory (KB) used by the process;
  • RSS The fixed amount of memory (KB) occupied by the process;
  • TTY On which terminal the process is running (the terminal location of the visitor). . If the value is PTS /0, the host process is connected to the network.
  • START Indicates the START time of the process.
  • TIME Indicates the actual CPU running TIME of the process.
  • COMMAND Specifies the COMMAND name and parameters.

STAT Status bit A common status character

  • D Uninterruptible sleep state (usually IO process);
  • R is running and can cross lines in the queue;
  • S is in hibernation;
  • T to stop or be tracked;
  • W into memory swapping (invalid since kernel 2.6);
  • X dead processes (very rare);
  • Z Zombie process;
  • < High priority process;
  • N A process with a lower priority.
  • L Some pages are locked in memory;
  • The leader of the s process (with child processes under it);
  • L Multi-process (using CLONE_THREAD, similar to NPTL pthreads);
    • A process group in the background;

Enter all process information intorumenz.txtfile

> ps aux > rumenz.txt
Copy the code

Output the specified field

> ps -o pid,ppid,pgrp,session,tpgid,comm
 PID  PPID  PGRP  SESS TPGID COMMAND
 2292 30905  2292 30905  2292 ps
30905 30899 30905 30905  2292 bash
Copy the code

ps auxandPs - auxThe difference between

Note that ps-AUX is different from Ps AUX. POSIX and UNIX standards require ps-AUX to print all processes for a user named x, as well as all processes that will be selected by the -a option. If the user name x does not exist, ps’s will be interpreted as Ps AUX and a warning will be printed. This behavior is intended to help transform old scripts and habits. It is fragile and is about to change, so it should not be relied upon.

ps auxps -ef

The output of the two is not very different, but the presentation style is different. Aux is BSD style, -ef is System V style. This is a minor difference, one that affects usage is that AUX truncates the command column while -ef does not. This difference affects the results when combined with grep.

Link :rumenz.com/rumenbiji/l… Wechat official Account: entry station