brief introduction
I have dealt with many programmers. These programmers may be familiar with several ways of writing for traversal, but they know nothing about the environment in which the written program is deployed. I bet few programmers know how tomcat works after spring boot. For them, running a jar package is done.
The advanced nature of tools really brings us many conveniences, and also improves the efficiency of programmers' development, and also reduces the entry threshold of programmers. Today, I want to discuss with you what the kill Command in linux is used for.
It is possible that many small partners first contact the Kill Command when their colleagues tell them 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 kill, which is to kill processes. Before killing the process, we need to find the process ID.
Generally, the ps command is used 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 forcibly kill the process.
Is there any further usage? Yes, let's have a look.
In depth usage of kill
Let's take a look at the command parameters of kill:
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, that is, the signal. In other words, the essence of kill is to transmit signals to programs.
If we use kill -l, we can get exactly how many signals kill can transmit:
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 64 signals in total. The signals may be different in different kill versions, but they basically cover the commonly used signals.
Here are the meanings of some common signals:
HUP 1 Terminal disconnection INT 2 Interrupt (same as Ctrl + C) QUIT 3 Exit (same as Ctrl + \) TERM 15 termination KILL 9 Compulsory termination CONT 18 Continue (with STOP contrary, fg/bg (command) STOP 19 Pause (same as Ctrl + Z)
What do you think of the version of kill?
/bin/kill --version kill from util-linux 2.23.2
If kill does not pass SIG, it will pass the default sig=TERM, that is, 15. Therefore, kill 54321 and kill -15 54321 above are equivalent.
In general, we prefer to use SIGTERM signal. This is because after the program receives SIGTERM signal, it will do some program cleaning operations, or graceful shutdown.
If you pass in kill -9, that is, SIGKILL, the application will not be able to capture this signal, resulting in the forced shutdown of the program, which may cause some exceptions, such as the data has not been saved, the data transmission has not 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 for anomaly detection.
pid is the process id, which can be understood as the process number. In addition to the process number, you can also pass in some special values, such as:
- 0 represents all processes of the current process group
- -1 indicates 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 the PID, we see that kill can also accept job specs. Job IDS can be listed using the jobs command.
Zombie process and kill
As mentioned above, the initial process with pid=1 cannot be killed. Another process that cannot be killed is called zombie process.
Zombie process is a very unique state in linux programs. It means that the process has ended, but it has not completely died, just like zombies.
The five major process states in linux are: RUNNING: RUNNING or waiting to run, uniterruptable: non interruptible blocking state, interruptible blocking state, STOPPED: suspended state and ZOMBIE: ZOMBIE state.
So what is the zombie process?
Zombie process refers to the process that does not disappear immediately after the program exits, but will retain a data structure called zombie. This data structure is very special because it has no memory space, no executable code, and of course it can not be scheduled. It just occupies a position in the process list and records all kinds of information when the process exits.
The zombie process mainly keeps the exit site of the process for analysis by the parent process or system administrator, so the zombie process is collected and released by the parent process. Because the zombie process has exited, it is useless to use kill. It can only exit after the parent process exits.
How to view 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 zombie s in it.
java thread dump
Another very useful aspect of kill is to generate the thread dump of the java program, dump the thread information of the current java program, and carry out some useful analysis, such as deadlock analysis.
How to do thread dump on a java process? Simply use the kill -3 Command:
kill -3 <pid>
From the above description, we can specify that the signal represented by 3 is SIGQUIT. This indicates that the JVM has built-in capture of this signal. If this signal is received, it will dump the current thread information.
java thread dump is very useful for thread analysis of java.
summary
This article introduces the in-depth usage of kill and the underlying working principle, as well as several applications of kill. I hope that the next time someone asks you what kill is, everyone can be very proud to tell him!