Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the kill command

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to understand kill commands". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand kill commands.

Kill command

As we all know, there are two ways to terminate a process in Linux: if it is a foreground process, you can use the Ctrl+C key; if it is a background process, you need to use the kill command to terminate. (in fact, Ctrl+C is also a kill command)

The format of the kill command is:

Kill [parameters] [process number] such as: kill 21121 kill-9 21121

The [parameter] is optional, and the process number can be obtained through tools such as jps/ps/pidof/pstree/top.

There are several command parameters for kill:

-l signal, if you do not add the numbering parameter of the signal, use the "- l" parameter to list all the signal names-a when processing the current process, there is no restriction on the correspondence between the command name and the process number-p specifies that the kill command only prints the process number of the relevant process, and does not send any signal-s specified send signal-u specified user

In general, we use-l (signal) more often, such as 9 in kill-9 mentioned earlier.

If the signal is not specified, a termination signal (15) is sent by default. The common signals are as follows:

HUP 1 terminal disconnect INT 2 interrupt (same as Ctrl + C) QUIT 3 exit (same as Ctrl +\) TERM 15 terminates KILL 9 forcibly terminates CONT 18 continues (as opposed to STOP, fg/bg command) STOP 19 pauses (same as Ctrl + Z)

The more commonly used are the forced termination signal: 9 and the termination signal: 15. In addition, the interrupt signal: 2 is actually the Ctrl+C termination foreground process that we mentioned earlier.

So what's the difference between kill-9 and kill-15? How to choose?

The difference between kill-9 and kill-15

The default signal for the kill command is 15. First, let's talk about the default kill-15 signal.

When kill-15:00 is used, the system sends a SIGTERM signal to the corresponding program. When the program receives the signal, it is up to it to decide how to deal with it.

At this point, the application can choose:

Stop the program immediately

Stop the program after releasing the response resource

Ignore the signal and continue to execute the program

Because the kill-15 signal only informs the corresponding process to have a "safe and clean exit", after the program receives the signal, it will generally do some "preparatory work" before exiting, such as resource release, temporary file cleaning, etc., and then terminate the program if the preparatory work is done.

However, if you encounter a block or other problem during the "preparatory work" that makes it impossible to succeed, the application can choose to ignore the termination signal.

This is why there is no way to "kill" an application with the kill command, because the default kill signal is SIGTERM (15), while the SIGTERM (15) signal can be blocked and ignored.

Compared with kill-15, kill-9 is relatively tough, the system will send a SIGKILL signal, he requires that the program that receives the signal should immediately stop running, can not be blocked or ignored.

Therefore, compared with the kill-15 command, the kill-9 application does not have time to "prepare" when it is executed, so this usually brings some side effects, such as data loss or terminal failure to return to normal state.

How does Java deal with SIGTERM (15)

As we all know, in Linux, the Java application runs as an independent process, and the termination of the Java program is based on the shutdown of JVM.

There are three ways to shut down JVM:

Normal shutdown: when the last non-daemon thread ends or calls System.exit or closes through other platform-specific methods (receives SIGINT (2), SIGTERM (15) signals, etc.)

Force shutdown: by calling the Runtime.halt method or by forcing kill in the operating system (receiving a SIGKILL (9) signal)

Abnormal shutdown: RuntimeException exception encountered in operation, etc.

When the JVM process receives the notification of kill-15 signal, it can do some cleaning actions, such as deleting temporary files, etc.

Of course, developers can customize to do some additional things, such as stopping the Tomcat container, taking the Dubbo service offline, and so on.

This way of customizing JVM cleanup actions is achieved through shutdown hook provided in JDK.

JDK provides a Java.Runtime.addShutdownHook (Thread hook) method to register a hook that JVM closes.

Examples are as follows:

Package com.hollis; public class ShutdownHookTest {public static void main (String [] args) {boolean flag = true; Runtime.getRuntime () .addShutdownHook (new Thread (()-> {System.out.println ("hook execute...");})) While (flag) {/ / app is runing} System.out.println ("main thread execute end...");}}

Execute the command:

➜jps 6520 ShutdownHookTest 6521 Jps➜ kill 6520

Console output:

Hook execute... Process finished with exit code 143( interrupted by signal 15: SIGTERM)

You can see that when we shut down the process using kill (the default kill-15), the program executes the shutdownHook I registered and then exits, and gives a prompt: interrupted by signal 15: SIGTERM.

If we execute the command kill-9:

➜kill-9 6520

Console output:

Process finished with exit code 137 (interrupted by signal 9: SIGKILL)

As you can see, when we use kill-9 to force the shutdown of the process, the program does not execute shutdownHook, but exits directly, and gives a prompt: interrupted by signal 9: SIGKILL.

At this point, I believe you have a deeper understanding of "how to understand the kill command". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report