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

What are the considerations for the use of kill commands in Java applications

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the precautions for the use of kill commands in Java applications". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the precautions for the use of kill commands in Java applications?"

Common signals in linux

1 SIGHUP suspend process

2 SIGINT terminates the process

3 SIGGQUIT stop the process

9 SIGKILL unconditionally terminates the process

15 SIGTERM terminate the process as much as possible

17 SIGSTOP unconditionally stop the process, but not terminate

18 SIGTSTP stops or pauses the process but does not terminate the process

19 SIGCONT continues to run stopped processes

Application in Java

Create a web application for SpringBoot

The startup class is as follows, adding a hook function that will be called when the process shuts down.

@ SpringBootApplicationpublic 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 to package.

Mvn package

Test kill-3

Start the application

Note here that if you start the application with a relative path, the process name seen by jps is jar, and the full jar name is displayed using the absolute path as follows: web-1.0.jar.

Java-jar ~ / aProject/web/target/web-1.0.jar

View process pid

Lgj@lgj-Lenovo-G470:~/aProject/web/target$ jps | grep web21060 web-1.0.jar

Use kill-3

Lgj@lgj-Lenovo-G470:~/aProject/web/target$ kill-3 21060

You can see that the startup interface prints the stack information of the Java application. The thread information is printed when the-3 signal is received.

If you start the application in the following ways

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 that the directory where the startup is located has an extra file nohup.out. This file records the log during the startup of the application.

Nohup command &

& means to run the application in the background. However, if you exit the console that closes the startup, the process will stop.

Nohup + & also runs the application in the background, but exits the console that closes the startup, and the process does not stop. And the process log will be output to nohup.out.

Kill-3 is used at this time. The printed thread information can be viewed in the nohup.out.

Test kill and kill-9

Start the application

Java-jar ~ / aProject/web/target/web-1.0.jar

Use kill pid to kill the process

Lgj@lgj-Lenovo-G470:~/aProject/web/target$ jps | grep web21470 web-1.0.jarlgj@lgj-Lenovo-G470:~/aProject/web/target$ kill 21470lgjoblgjlgjlyLenovoluthG470 renovated a ProjectAccording to webtarget $

You can see what is printed in the hook function in the output log

Do ShutdownHook.

2019-06-09 01 INFO 1815 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 o.s.s.concurrent.ThreadPoolTaskExecutor 18 o.s.s.concurrent.ThreadPoolTaskExecutor 41.381 INFO 21470-[Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor: Shutting down ExecutorService 'applicationTaskExecutor'

Use kill-9 pid to kill the process

Lgj@lgj-Lenovo-G470:~/aProject/web/target$ jps | grep web21568 web-1.0.jarlgj@lgj-Lenovo-G470:~/aProject/web/target$ kill-9 21568

The final log does not output the contents of the hook function.

2019-06-09 01 INFO 20 Tomcat started on port 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 o.s.b.w.embedded.tomcat.TomcatWebServer 20 Tomcat started on port 37.585 INFO 21568-[main] com.demo.web.WebApplication: Started WebApplication in 4.171 seconds (JVM running for 4.812) Killed

At this point, I believe you have a deeper understanding of "what matters needing attention in the use of kill commands in Java applications". 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