Introduction
I have dealt with many programmers. These programmers may be familiar with several ways of writing for traversal, but they don't know anything about the environment in which the programs they write are deployed. I bet that after the emergence of spring boot, very few programmers know how tomcat works. For them, running a jar package is all done.
The advanced nature of the tools does bring us a lot of convenience, it also improves the programmer's development efficiency, and at the same time lowers the programmer's entry barrier. Today I want to discuss with you, what exactly is the kill command in linux.
It may be the first time that many friends contact the kill command when a colleague tells him to kill the process. So is kill really used to kill the process?
Use kill to kill the process
Let's first look at the most basic and common application of a kill is to kill a process. Before killing the process, we need to find the process ID.
Under normal circumstances, use the ps command to find the process ID. Join this process ID=54321.
Then you can use kill 54321 to kill the process.
More senior students may also use kill -9 54321 to forcefully kill this process.
Is there a more in-depth usage? Yes, let's take a look.
In-depth usage of kill
Let's take a look at what the kill command parameters are:
kill
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
You can see that the parameter of kill is sig, which is the signal. In other words, the essence of kill is to pass signals to the program.
If we use kill -l, we can get how many signals kill can pass:
kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
There are a total of 64 signals. Different kill versions may have different signals, but they basically cover commonly used signals.
The following are the meanings of some commonly used signals:
HUP 1 终端断线
INT 2 中断(同 Ctrl + C)
QUIT 3 退出(同 Ctrl + \)
TERM 15 终止
KILL 9 强制终止
CONT 18 继续(与STOP相反, fg/bg命令)
STOP 19 暂停(同 Ctrl + Z)
How do you see the kill version?
/bin/kill --version
kill from util-linux 2.23.2
If kill does not pass sig, then the default sig=TERM will be passed, which is 15. So the above kill 54321 and kill -15 54321 are equivalent.
Under normal circumstances, we prefer to use the SIGTERM signal. This is because when the program receives the SIGTERM signal, it will do some cleanup operations, or gracefully shut down.
If kill -9 is passed in, that is, SIGKILL, the application program will not be able to catch this signal, which will cause the program to be forcibly closed, which may result in some abnormal situations, such as data has not been saved, data transmission has not yet ended, and so on.
sig also has a special value called 0. If 0 is passed in, the actual signal will not be sent. This is only used for anomaly detection.
The pid is the process id, which can be understood as the process number. In addition to the process ID, you can also pass in some special values, such as:
- 0 means all processes in the current process group
- -1 means all processes with PID>1
There is also a special pid=1. This pid represents the initial process init, which cannot be killed.
In addition to PID, we see that kill can also accept jobspec. The job id can be listed using the jobs command.
Zombie process and kill
As mentioned above, the initial process with pid=1 cannot be killed. There is also a process that cannot be killed is called a zombie process.
The zombie process is a very unique state in the linux program, it means that the process has ended, but it has not completely died, just like a zombie.
The five major process states in linux are: RUNNING: running or waiting to run state, UNINTERRUPTABLE: uninterruptible blocking state, INTERRUPTABLE: interruptible blocking state, STOPPED: suspended state and ZOMBIE: zombie state.
So what is a zombie process?
The zombie process means that after the program exits, the process does not disappear immediately, but retains a data structure called a zombie. This data structure is very special because it has no memory space, no executable code, and of course it cannot be scheduled. It just occupies a position in the process list and records various information when the process exits.
The zombie process mainly retains the exit scene of the process for the parent process or system administrator to analyze and use, so the zombie process is handed over to the parent process for collection and release. Because the zombie process has already exited, it is useless to use kill. You can only wait for its parent process to exit before you can actually exit.
How to check the zombie process? The easiest way is to use the top command:
top - 14:34:38 up 305 days, 4:23, 2 users, load average: 0.20, 0.29, 0.47
Tasks: 93 total, 1 running, 92 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.0 us, 0.7 sy, 0.0 ni, 97.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1882008 total, 525524 free, 311440 used, 1045044 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 1382560 avail Mem
In the above output, we can see that there are 0 zombies in it.
java thread dump
Another very useful place for kill is to generate a thread dump of a java program, dump the thread information of the current java program, and perform some useful analysis, such as deadlock analysis.
How to do thread dump to java process? It is very simple to use the kill -3 command:
kill -3 <pid>
From our introduction above, we can specify that the signal represented by 3 is SIGQUIT. This shows that the JVM has built-in capture of this signal. If this signal is received, the current thread information will be dumped.
Java thread dump is very useful for thread analysis of java.
Summarize
This article introduces the in-depth usage of kill and the underlying working principle. It also introduces several applications of kill. I hope that next time someone asks you what kill is, everyone can be very proud to tell him!
This article has been included in http://www.flydean.com/01-that-is-kill/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。