知乎专栏 |
kill 命令本质是给进程发送终止信号,进程接收到终止信号后退出运行。
可以看到 Springboot 启动后,进程 PID 44559,现在使用 kill 命令杀死这个进程
当执行 kill 44559 你会看到下面的输出
neo@MacBook-Pro-Neo ~/workspace/microservice/test % java -jar target/test-0.0.1-SNAPSHOT.jar Starting... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.3) 2021-07-29 11:05:09.862 INFO 44559 --- [ main] cn.netkiller.Application : Starting Application v0.0.1-SNAPSHOT using Java 16.0.1 on MacBook-Pro-Neo.local with PID 44559 (/Users/neo/workspace/microservice/test/target/test-0.0.1-SNAPSHOT.jar started by neo in /Users/neo/workspace/microservice/test) 2021-07-29 11:05:09.865 INFO 44559 --- [ main] cn.netkiller.Application : No active profile set, falling back to default profiles: default 2021-07-29 11:05:11.363 WARN 44559 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used 2021-07-29 11:05:11.399 INFO 44559 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext 2021-07-29 11:05:11.400 INFO 44559 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1451 ms 2021-07-29 11:05:12.041 INFO 44559 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' 2021-07-29 11:05:12.073 INFO 44559 --- [ main] io.undertow : starting server: Undertow - 2.2.9.Final 2021-07-29 11:05:12.085 INFO 44559 --- [ main] org.xnio : XNIO version 3.8.4.Final 2021-07-29 11:05:12.099 INFO 44559 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.8.4.Final 2021-07-29 11:05:12.197 INFO 44559 --- [ main] org.jboss.threads : JBoss Threads version 3.1.0.Final 2021-07-29 11:05:12.263 INFO 44559 --- [ main] o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 8080 (http) 2021-07-29 11:05:12.278 INFO 44559 --- [ main] cn.netkiller.Application : Started Application in 2.989 seconds (JVM running for 3.582) 2021-07-29 11:05:20.577 INFO 44559 --- [ionShutdownHook] io.undertow : stopping server: Undertow - 2.2.9.Final ============================== Destroying Spring ==============================
而是用 kill -9 PID 就不会出现下面提示。
neo@MacBook-Pro-Neo ~/workspace/microservice/test % java -jar target/test-0.0.1-SNAPSHOT.jar Starting... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.3) 2021-07-29 11:08:10.857 INFO 44613 --- [ main] cn.netkiller.Application : Starting Application v0.0.1-SNAPSHOT using Java 16.0.1 on MacBook-Pro-Neo.local with PID 44613 (/Users/neo/workspace/microservice/test/target/test-0.0.1-SNAPSHOT.jar started by neo in /Users/neo/workspace/microservice/test) 2021-07-29 11:08:10.860 INFO 44613 --- [ main] cn.netkiller.Application : No active profile set, falling back to default profiles: default 2021-07-29 11:08:12.377 WARN 44613 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used 2021-07-29 11:08:12.411 INFO 44613 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext 2021-07-29 11:08:12.411 INFO 44613 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1466 ms 2021-07-29 11:08:13.046 INFO 44613 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' 2021-07-29 11:08:13.081 INFO 44613 --- [ main] io.undertow : starting server: Undertow - 2.2.9.Final 2021-07-29 11:08:13.100 INFO 44613 --- [ main] org.xnio : XNIO version 3.8.4.Final 2021-07-29 11:08:13.114 INFO 44613 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.8.4.Final 2021-07-29 11:08:13.206 INFO 44613 --- [ main] org.jboss.threads : JBoss Threads version 3.1.0.Final 2021-07-29 11:08:13.275 INFO 44613 --- [ main] o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 8080 (http) 2021-07-29 11:08:13.290 INFO 44613 --- [ main] cn.netkiller.Application : Started Application in 3.195 seconds (JVM running for 3.808) [1] 44613 killed java -jar target/test-0.0.1-SNAPSHOT.jar
这是因为 kill 命令会给进程发送终止信号,进程会正常退出.
什么是正常退出呢?例如:
这就是为什么当我们正常关闭程序需要等待很长时间,如果我们此时没有运行状态显示,也没有通过日志反应执行状态,就会认为程序死了。其实此时程序可能尽职尽责的在工作,将未完成的工作完成,然后一步步正常退出。
尤其是多线程的程序,退出时需要等待每个线程完成请求,需要很长时间,我们常常因为升级时间紧迫而使用 kill -9 强行杀死进程,这会带来很多问题。
kill -9 的弊端:
Ctrl + C 与 kill 没有区别,也是给进程发送终止信号,现在我们来演示一下。
neo@MacBook-Pro-Neo ~/workspace/microservice/test % java -jar target/test-0.0.1-SNAPSHOT.jar Starting... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.3) 2021-07-29 11:04:42.657 INFO 44546 --- [ main] cn.netkiller.Application : Starting Application v0.0.1-SNAPSHOT using Java 16.0.1 on MacBook-Pro-Neo.local with PID 44546 (/Users/neo/workspace/microservice/test/target/test-0.0.1-SNAPSHOT.jar started by neo in /Users/neo/workspace/microservice/test) 2021-07-29 11:04:42.660 INFO 44546 --- [ main] cn.netkiller.Application : No active profile set, falling back to default profiles: default 2021-07-29 11:04:44.212 WARN 44546 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used 2021-07-29 11:04:44.246 INFO 44546 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext 2021-07-29 11:04:44.246 INFO 44546 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1502 ms 2021-07-29 11:04:44.857 INFO 44546 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' 2021-07-29 11:04:44.889 INFO 44546 --- [ main] io.undertow : starting server: Undertow - 2.2.9.Final 2021-07-29 11:04:44.902 INFO 44546 --- [ main] org.xnio : XNIO version 3.8.4.Final 2021-07-29 11:04:44.916 INFO 44546 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.8.4.Final 2021-07-29 11:04:45.002 INFO 44546 --- [ main] org.jboss.threads : JBoss Threads version 3.1.0.Final 2021-07-29 11:04:45.068 INFO 44546 --- [ main] o.s.b.w.e.undertow.UndertowWebServer : Undertow started on port(s) 8080 (http) 2021-07-29 11:04:45.084 INFO 44546 --- [ main] cn.netkiller.Application : Started Application in 3.149 seconds (JVM running for 3.748) ^C2021-07-29 11:04:47.082 INFO 44546 --- [ionShutdownHook] io.undertow : stopping server: Undertow - 2.2.9.Final ============================== Destroying Spring ==============================