Linux Basics learning - process management (pstree, ps, pgrep, kill, killall, pkill, / proc, / sys)

concept

Procedures and processes

program

Command + parameters to accomplish a specific task by running the logic control

process

The output from running up
the basic unit of the operating system running
life cycle (generated ----> death)
process has run state (running, sleeping, waiting, dead)
concurrency, interaction (pipeline) ---- Shared Memory the queue (kafka)
program and the process is not one to one, the same program run on different data sets is a different process

Child and parent process

The child process will inherit the parent process characteristics: security authentication; past and current resources privileged file descriptors, ports, etc.; environmental variables; program code.
Parent ------> fork () ------> child process
child process ------> exec () ------> to run the program
the child ------ > exit () ------> exit
Here Insert Picture Description

Processes and Threads

For linux, do not strictly distinguish, for the process where the thread will have a separate set of complete set of resources (memory ----> virtual memory, CPU ----> time slice)

Linux process management tools

pstree see the relationship between process tree

-p  Also listed for each process PID

-A  connection between the process tree to the ASCII characters to connect
-U  connection between the process tree to utf8 characters to connect, there may be some errors terminal
-u  also listed for each process belongs to the account name

ps display operating system processes dynamically moment

ps  aux

Please click to see the ps command

pgrep to get the process of being scheduled information

1.pgrep program by matching its name, find the matching process
the later start time 2. The larger the process ID, does not necessarily mean that the process of

pgreep  view the process ID
pgrep -o  process ID minimum (parent)
pgrep -n  maximum process ID (the child)
pgrep the -l  all processes and names

[root@zycentos7 ~]# pgrep sshd
6836
7120
7128
[root@zycentos7 ~]# pgrep -o sshd
6836
[root@zycentos7 ~]# pgrep -n sshd
7128
[root@zycentos7 ~]# pgrep -l sshd
6836 sshd
7120 sshd
7128 sshd

killall,kill,pkill

killall to kill the process specified name

killall -i interactive mode
killall -l List all known signal names

Numbering Signal name
1) SIGHUP Reload Configuration
2) SIGINT Keyboard interrupt Ctrl + c
3) SIGQUIT Keyboard exit
9) SIGKILL Forced termination, may be a problem
15) SIGTERM Terminated (normal termination), default signal
18) SIGCONT carry on
19) SIGSTOP stop
20) SIGTSTP Pause ^ Z

Example 1 After SIGHUP signal is issued, the process number does not change the main process, the child process sends a Change No.

[root@zycentos7 ~]# ps -ef|grep nginx
root      31943      1  0 20:29 ?        00:00:00 nginx: master process nginx
nginx     31944  31943  0 20:29 ?        00:00:00 nginx: worker process
root      31948  28945  0 20:29 pts/2    00:00:00 grep --color=auto nginx
[root@zycentos7 ~]# kill -1 31943
[root@zycentos7 ~]# ps -ef|grep nginx
root      31943      1  0 20:29 ?        00:00:00 nginx: master process nginx
nginx     31982  31943  0 20:30 ?        00:00:00 nginx: worker process
root      31989  28945  0 20:30 pts/2    00:00:00 grep --color=auto nginx

Example 2 keyboard interrupt

[root@zycentos7 ~]# tty
/dev/pts/1
[root@zycentos7 ~]# ping 192.168.232.125
PING 192.168.232.125 (192.168.232.125) 56(84) bytes of data.
64 bytes from 192.168.232.125: icmp_seq=1 ttl=64 time=0.052 ms
……
64 bytes from 192.168.232.125: icmp_seq=29 ttl=64 time=0.079 ms
--- 192.168.232.125 ping statistics ---
29 packets transmitted, 29 received, 0% packet loss, time 28006ms
rtt min/avg/max/mdev = 0.052/0.168/2.452/0.432 ms

[root@zycentos7 ~]# tty
/dev/pts/2
[root@zycentos7 ~]# ps -ef|grep ping
root      29022  28172  0 19:33 pts/1    00:00:00 ping 192.168.232.125
root      29029  28945  0 19:33 pts/2    00:00:00 grep --color=auto ping
[root@zycentos7 ~]# kill -2 29022

Example 3 distinction kill -9 and kill -15 signal

[root@zycentos7 ~]# tty
/dev/pts/0
[root@zycentos7 ~]# ps -ef|grep vim
root      31480  28172  0 20:20 pts/1    00:00:00 vim test01
root      31489  28945  0 20:20 pts/2    00:00:00 vim test02
root      31503  31413  0 20:20 pts/0    00:00:00 grep --color=auto vim
[root@zycentos7 ~]# kill -9 31480
[root@zycentos7 ~]# kill -15 31489
[root@zycentos7 ~]# ps -ef|grep vim
root      31537  31413  0 20:21 pts/0    00:00:00 grep --color=auto vim

[root@zycentos7 ~]# tty
/dev/pts/1
[root@zycentos7 ~]# vim test01
Killed
[root@zycentos7 ~]# vim test01
E325: ATTENTION
Found a swap file by the name ".test01.swp"
          owned by: root   dated: Sat Nov 16 20:20:05 2019
         file name: ~root/test01
          modified: no
         user name: root   host name: zycentos7
        process ID: 31480
While opening file "test01"
             dated: Sat Nov 16 19:42:46 2019
[root@zycentos7 ~]# ls -al .test01.swp	;删除该文件后可正常打开
-rw-r--r--. 1 root root 12288 Nov 16 20:20 .test01.swp

[root@zycentos7 ~]# tty
/dev/pts/2
[root@zycentos7 ~]# vim test02
Vim: Caught deadly signal TERM
Vim: Finished.
Terminated
[root@zycentos7 ~]# vim test02	;可正常打开

killall -q not output a warning message

[root@zycentos7 ~]# killall nginx
nginx: no process found
[root@zycentos7 ~]# killall -q  nginx
[root@zycentos7 ~]# echo $?
1

killall -s transmission signal designated

killall -s HUP named
killall -HUP named

Whether killall -v report has successfully been sent
killall -9 named && killall named  to kill all processes with the same name
if the need to kill the process need to find, you need to use commands like ps together with grep to find the process to kill, killall can put two a combined process

kill kill the specified process PID

1. Typically, termination of a foreground process can use Ctrl + C keys.
2. terminate a background process with the kill command: you need to use the ps / pidof / pstree / top tools such as acquisition process PID, and then use the kill command to kill the process.
3.kill corresponding command is to end the process by sending a signal to the specified process. By default, the use of numbered signal TERM 15, TERM signal will terminate all can not capture the signal process. For those who can capture the signal process necessary for the kill signal with the number 9, forced to "kill" the process.

kill [选项] [进程id]

-l signal  Ruoguo signal parameters without number, use the -l option will list the names of all signals
-a  when dealing with the current process, does not limit the command name and the process ID of the correspondence between
-p  specify the kill command to print only related processes the process ID without sending any signal
-s  Specifies to send the signal
-u  specifies the user
focus

  1. Ordinary users can only kill command with no parameters or by using up signal -9 signal.
  2. After successfully sending the kill signal, shell will show the process of terminating the information on the screen. Sometimes this information is not immediately displayed, press the Enter key only when the shell command prompt appears again, will be displayed.
  3. kill -9 signal is forced to terminate the process, often cause some side effects, such as loss of data or terminal can not be restored to normal.
  4. To undo all the background work, you can enter killall or kill 0, kill 0 can be used to detect whether the process exists.

Example 1 List all signal names

[root@zycentos7 ~]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 ……
63) SIGRTMAX-1  64) SIGRTMAX

Example 2 to obtain the specified value signal

[root@zycentos7 ~]# kill -l KILL
9

Example 3 to find the process with ps, then killed with kill

[root@zycentos7 ~]# ps -ef|grep vim
root       7981   7964  0 09:27 pts/1    00:00:00 vim 1
root       7983   7740  0 09:28 pts/0    00:00:00 grep --color=auto vim
[root@zycentos7 ~]# kill 7981
[root@zycentos7 ~]# echo $?
0

Example 4 completely kill the process

[root@zycentos7 ~]# ps -ef|grep vim
root       7987   7964  0 09:30 pts/1    00:00:00 vim 1
root       7989   7740  0 09:30 pts/0    00:00:00 grep --color=auto vim
[root@zycentos7 ~]# kill -9 7987

pkill

ps and binding is pkill command kill command, according to the process name to kill the specified process, a signal may be transmitted
pkill -l pts/0by the terminal kill process
pkill -9 -l pts/0a transmission signal 9, a terminal and killed
pkill -u userby the user to kill the process
pkill -19 -u user

htop real-time monitoring system processor state

Please click to see the top command

[root@zycentos7 ~]# yum install htop -y

[Enhanced version of monitoring software top Htop, compared to top its own has a lot of advantages]
1. When comparing the two together, top cumbersome
2. By default graphical interface supports mouse
3. You can scroll through the horizontal or vertical list of processes, to see all processes and complete command line
without entering the process number 4. kill processes, etc.

Here Insert Picture Description
Area on the left  shows the information CPU, physical memory and swap partitions
the right area  shows the number of tasks, the average load and run-time connection information such as
the process of regional  shows all processes currently in the system  and show a consistent top

PID:进行的标识号
USER:运行此进程的用户
PRI:进程的优先级
NI:进程的优先级别值,默认的为0,可以进行调整
VIRT:进程占用的虚拟内存值
RES:进程占用的物理内存值
SHR:进程占用的共享内存值
S:进程的运行状况,R表示正在运行、S表示休眠,等待唤醒、Z表示僵死状态
%CPU:该进程占用的CPU使用率
%MEM:该进程占用的物理内存和总内存的百分比
TIME+:该进程启动后占用的总的CPU时间
COMMAND:进程启动的启动命令名称

Taiwan before and after the process

Reception can only run one process, one can run multiple background processes
to execute a foreground program in the background&

[root@zycentos7 ~]# ping www.baidu.com >/dev/null 2>&1 &
[1] 32627
[root@zycentos7 ~]# ps -ef|grep ping
root      32627  28945  0 20:42 pts/2    00:00:00 ping www.baidu.com
root      32636  28945  0 20:42 pts/2    00:00:00 grep --color=auto ping
[root@zycentos7 ~]# ping www.baidu.com >/dev/null 2>&1 &
[2] 32652
[root@zycentos7 ~]# ps -ef|grep ping
root      32627  28945  0 20:42 pts/2    00:00:00 ping www.baidu.com
root      32652  28945  0 20:43 pts/2    00:00:00 ping www.baidu.com
root      32656  28945  0 20:43 pts/2    00:00:00 grep --color=auto ping

Process State

sleep

  • (S) can interrupt sleep
  • (D) uninterruptible sleep

running

  • (ready)
  • (Kernel)
  • (user)

stop
zombie

  • (Z) resources have been cleared
  • (X) resources are not cleaned up

Switching between process states

run Ready wait
Operations on CPU Allocated resources, CPU idle waiting And does not trigger

Run ---- 时间片耗尽----> Ready
wait ---- 事件触发----> Ready
Ready ---- 进程调用或者系统调用----> Run
Run ---- 等待事件发生----> wait

Process priority

nice value (NI)  relative priority (priority metric) -20 ~ + 19, nice lower the value the higher priority on Linux [O1 scheduling using the original called nice static state priority]
priorty value (PR)  to nice level value mapped to a larger display priority queue 100 maps to -20, + 19 139 [mapped to O1 on the original schedule using the Linux priorty called dynamic state priority]
0-99 (100) the priority system process grade (this process is the range of values of real-time process, implies a message: the current system is a Linux operating system already supports real-time process)
priority (non-real-time process) 100-139 (40) user process

Use ps nice view level, using the top-level view PR

[root@zycentos7 ~]# ps axo pid,command,nice --sort=-nice
PID COMMAND                      NI
    32 [khugepaged]                 19
    31 [ksmd]                        5

NI - resident process, no priority
specified nice value

[root@zycentos7 ~]# nice -n -5 sleep 600 &
[4] 36673
[root@zycentos7 ~]# ps axo command,pid,nice|grep sleep
sleep 600                    36673  -5
grep --color=auto sleep      36706   0

Changing the value renice nice

[root@zycentos7 ~]# renice -20 PID

Job Control

operation

  1. In a statement on the implementation of the terminal, it is a job; multiple processes to complete a task, that is a job
  2. For the shell do not control the process, make process control needs to kill, killall, pkill, top send signals, job control shell is

The difference between processes and operations

  1. Difference: the process is a program that once executed on a data set, but the job is submitted to a user task system.
  2. Relationship: A job usually includes several processes, several processes together to complete a task, that is, jobs.
  3. After a user submits a job, when the job is scheduled, the system will create a process for the job, when a process can not be completed, the system will create a child process for this process.

Job Control

shell can run any of a plurality of foreground and background jobs job, referred to as job control

operating meaning
jobs View Job
Ctrl+z The foreground program hangs (pause) to the background
fg %# Recovery continues to run to the front
bg %# Recovery continues to run background
kill -9 %# The default transmit signal 15
[root@zycentos7 ~]# sleep 600 &
[1] 7838
[root@zycentos7 ~]# sleep 600
^Z
[2]+  Stopped                 sleep 600
[root@zycentos7 ~]# jobs
[1]-  Running                 sleep 600 &
[2]+  Stopped                 sleep 600
[root@zycentos7 ~]# ps aux|grep sleep
root       7838  0.0  0.0 107956   356 pts/0    S    10:08   0:00 sleep 600
root       7841  0.0  0.0 107956   356 pts/0    T    10:09   0:00 sleep 600
root       7844  0.0  0.0 112712   964 pts/0    R+   10:10   0:00 grep --color=auto slee
[root@zycentos7 ~]# fg 2
sleep 600
^Z
[2]+  Stopped                 sleep 600
[root@zycentos7 ~]# jobs
[1]-  Running                 sleep 600 &
[2]+  Stopped                 sleep 600
[root@zycentos7 ~]# bg 2
[2]+ sleep 600 &
[root@zycentos7 ~]# jobs
[1]-  Running                 sleep 600 &
[2]+  Running                 sleep 600 &
[root@zycentos7 ~]# kill %1
[root@zycentos7 ~]# jobs
[1]-  Terminated              sleep 600
[2]+  Running                 sleep 600 &
[root@zycentos7 ~]# jobs
[2]+  Running                 sleep 600 &
[root@zycentos7 ~]# kill -9 %2
[root@zycentos7 ~]#
[2]+  Killed                  sleep 600

/ Proc and / sys introduced

General

/ proc  kernel information mapped running

  1. Process Information
  2. Memory resource information
  3. Disk partition information

/ sys  drivers for hardware devices information

/ Proc file system

Linux kernel provides a mechanism for the / proc file system access internal kernel data structures at runtime, change the kernel settings. proc file system is a pseudo file system, it exists only memory of them, and do not take up external memory space . It is the operating system kernel to access the data in a manner that provides an interface to the file system.

  1. / Proc file under basically read-only, in addition to / proc / sys directory, which is writable (view and modify the kernel of the operating parameters)
  2. / Proc directory is the digital command for the PID process directory
file meaning
/proc/cpuinfo cpu information (lscpu)
/proc/meminfo Memory usage information (free -m)
/proc/uptime cpu load (uptime)
/proc/cmdline Kernel boot parameter information
/proc/devices The device has been loaded and classification
/proc/filesystems Kernel file system types currently supported
/proc/loadavg Load state, and related uptime command
/proc/modules A list of all modules loaded into the kernel
/proc/mounts All mounts used in the system
/ Proc / State All CPU activity information

/ Sys file system

Sysfs is similar to a special file system proc file system for the device in the system organized into a hierarchy, and provides detailed information about user mode kernel data structure of the program . I.e. in user mode can access the file system sys, some drivers or other terms kernel mode device.

File or directory meaning
/ Sys / fs Description of the system in all file systems, including file system itself and according to the file system mount points have been classified storage
/sys/kernel All kernel adjustable parameters
Published 43 original articles · won praise 30 · views 5933

Guess you like

Origin blog.csdn.net/qq_42049496/article/details/103160226