Foreword
As we all know, kill is used to kill process linux system.
kill pid [..]
The kill command may be sent to the corresponding predetermined signal process or work. The default is to use the kill command signal 15 for the end of the process or work. If the process or working ignore this signal, you can use the signal 9, forced to kill the process or job.
Therefore, to ensure that the process to kill if you can use a -9 argument
kill -9 pid [..]
The use -3 parameters, you can print process execution log
kill -3 pid
Applications in Java
Create a web application of SpringBoot
Start classes are as follows, add a hook function, when the process closed, will call the hook function.
@SpringBootApplication public class WebApplication { public static void main(String args[]){ SpringApplication.run(WebApplication.class,args); Runtime.getRuntime().addShutdownHook(new Thread(){ @Override public void run() { System.out.println("do ShutdownHook.......... "); } }); } }
Use maven package.
mvn package
Testing kill -3
Start the application
Be careful if you use relative paths to start applications, jps see the name of the process for the jar, using an absolute path follows the full jar name is displayed: web-1.0.jar.
java -jar ~/aProject/web/target/web-1.0.jar
View the process pid
lgj@lgj-Lenovo-G470:~/aProject/web/target$ jps | grep web 21060 web-1.0.jar
Use kill -3
lgj@lgj-Lenovo-G470:~/aProject/web/target$ kill -3 21060
You can see the start screen printed stack information Java applications. Print signal is received -3 time thread information.
If you use the following way to start the application
nohup java -jar ~/aProject/web/target/web-1.0.jar &
lgj@lgj-Lenovo-G470:~/aProject/web/target$ ls -l |grep nohup.out -rw------- 1 lgj lgj 22811 Jun 9 00:41 nohup.out
You can see the start of the directory where more than one file nohup.out. The document records the application process to start running log.
nohup command &;
Saying that he sets run applications.
At this time, using kill -3. Thread information can be printed to the nohup.out view.
Testing kill and kill -9
Start the application
java -jar ~/aProject/web/target/web-1.0.jar
Use kill the process kill pid
lgj@lgj-Lenovo-G470:~/aProject/web/target$ jps | grep web 21470 web-1.0.jar lgj@lgj-Lenovo-G470:~/aProject/web/target$ kill 21470 lgj@lgj-Lenovo-G470:~/aProject/web/target$
You can see the output log output function hooks printed
do ShutdownHook..........
2019-06-09 01:18:28.610 INFO 21470 --- [ main] com.demo.web.WebApplication : Started WebApplication in 4.585 seconds (JVM running for 5.274) do ShutdownHook.......... 2019-06-09 01:18:41.381 INFO 21470 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Use kill -9 pid kill the process
lgj@lgj-Lenovo-G470:~/aProject/web/target$ jps | grep web 21568 web-1.0.jar lgj@lgj-Lenovo-G470:~/aProject/web/target$ kill -9 21568
The last log does not output the contents of the hook function
2019-06-09 01:20:37.579 INFO 21568 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8452 (http) with context path '' 2019-06-09 01:20:37.585 INFO 21568 --- [ main] com.demo.web.WebApplication : Started WebApplication in 4.171 seconds (JVM running for 4.812) Killed
to sum up
When using the kill operation java application
1.kill -3 pid can print thread information of the current process, but does not close the Java application!
2.kill pid is kill -15 pid, will call the hook function ShutdownHook , general ShutdownHook will be some operations, such as saving data, close the connection.
3.kill -9 pid. Does not call the hook function ShutdownHook .