Method for automatically restarting web service based on centos
By configuring elegant shutdown and thread pool waiting in Spring Boot applications, and automatically restarting in combination with CentOS service file configuration, the data inconsistency caused by forced termination of processes in Java Web services is solved, and the stability and manageability of the Web service are improved.
Patent Information
- Application Number
- CN202411627510.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-14
- Publication Date
- 2025-03-21
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The prior art uses the kill-9 command to force the process to be terminated in Java Web services, which may lead to the risk of data inconsistency, and service restarts require the use of third-party monitoring tools to provide complex installation and configuration.
By configuring elegant shutdown and thread pool waiting in the application.properties of Spring Boot application, configuring automatic restart in combination with CentOS service files, and operating the service using the systemctl command, achieving elegant termination and automatic restart.
It improves the stability and manageability of web services, ensures data consistency and resource release integrity, and provides users with a convenient and efficient operation experience.
Smart Images

Figure CN119668698A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of web technology, and in particular to a method for automatically restarting a web service based on CentOS. Background Art
[0002] Nowadays, most Java Web services choose to be deployed in CentOS systems based on the Spring Boot framework. However, in practice, many people are accustomed to using the kill-9 command to forcibly terminate the running Java process and restart the service. As we all know, when the operating system receives the kill-9 command, it will send a SIGKILL signal to the specified process. This signal is the only signal that cannot be ignored. It will immediately terminate the process regardless of what operation the process is currently performing. However, this forced termination may cause the risk of data inconsistency, especially when the process is performing critical disk or database write operations. At the same time, after the service process is terminated unexpectedly, it is usually necessary to use third-party monitoring tools to detect and restart the Java program, and the installation and configuration of these tools may be relatively complicated.
[0003] Therefore, the prior art has defects and needs to be improved. Summary of the invention
[0004] The technical problem to be solved by the present invention is: to provide a method for automatically restarting a web service based on centos, thereby improving the stability and manageability of the web service and providing users with a more convenient and efficient operating experience.
[0005] The technical solution of the present invention is as follows: a method for automatically restarting a web service based on centos is provided, comprising the following steps.
[0006] S1: Modify the Spring Boot application.properties configuration; specifically: In Java services based on SpringBoot version 2.3.0 or later, add the following configuration to the application.properties configuration file to achieve graceful shutdown of the service:
[0007] server.shutdown=graceful
[0008] spring.lifecycle.timeout-per-shutdown-phase=Ns;
[0009] The above configuration sets the service shutdown mode to graceful shutdown and sets the shutdown waiting time to N seconds. N is a specific value, such as 5, 8, 10, 11, 15, 20, etc.
[0010] S2: Modify the Spring Boot thread pool configuration; specifically: inside the Spring Boot service, set all asynchronous thread pools to wait for the task to complete before terminating the thread pool, and set the waiting timeout to N seconds to avoid infinite waiting; at the same time, listen for service shutdown notifications, and call the thread pool or thread shutdown method after receiving the notification;
[0011] S3: Configure the service configuration file; specifically: create a service file and configure it according to the CentOS service file specification; the service file has the suffix .service and contains the following configuration:
[0012] [Unit]
[0013] Description = MyService
[0014] After=syslog.target network-online.target remote-fs.target nss-lookup.target
[0015] [Service]
[0016] Type=forking
[0017] ExecStart= / opt / java / bin / xxx.sh
[0018] ExecStop= / bin / kill-15$MAINPID
[0019] RemainAfterExit=yes
[0020] StandardOutput = journal
[0021] Restart=always
[0022] RestartSec=N
[0023] [Install]
[0024] WantedBy=multi-user.target;
[0025] ExecStart refers to the path of the web service startup script, Restart=always means the service is always restarted, RestartSec=Ns means the service will restart after N seconds, and ExecStop= / bin / kill-15$MAINPID means the service uses kill-15 to stop the process. xxx is the name of the service file.
[0026] S4: Use systemctl start to test the service; specifically: place the service file in the system path / lib / systemd / system / ; and check whether the service is successfully started by executing the systemctl start xxx command, where xxx is the name of the service file.
[0027] Furthermore, the Web service is made into a system service and included in the monitoring scope of the system; the Web service is operated through simple and convenient system commands (such as systemctl restart, stop, etc.) to realize the automatic restart function.
[0028] Furthermore, a SIGTERM signal is sent to the process to request it to exit gracefully, thereby ensuring data consistency and integrity of resource release.
[0029] Furthermore, the automatic restart technology based on CentOS 7.0 is used for the reliable operation of Web services.
[0030] Adopting the above scheme, the web service of the present invention is based on the method of automatic restart of centos, and the running process is gracefully terminated by using the kill-15 command. By sending a SIGTERM signal to the process, it is requested to exit gracefully to ensure data consistency and integrity of resource release. In addition, by making the Web service as a system service and incorporating it into the monitoring scope of the system. In this way, the user can operate the Web service through simple and convenient system commands (such as systemctl restart, stop, etc.) to realize the automatic restart function. The present invention significantly improves the stability and manageability of the Web service, and provides users with a more convenient and efficient operation experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 The present invention is a flow chart of the method. DETAILED DESCRIPTION
[0032] The present invention is described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0033] See also Figure 1 The present invention provides a method for automatically restarting a web service based on centos, comprising the following steps.
[0034] S1: In Java services based on Spring Boot 2.3.0 and above, add the following configuration to the application.properties configuration file to achieve graceful shutdown of the service:
[0035] server.shutdown=graceful
[0036] spring.lifecycle.timeout-per-shutdown-phase=Ns;
[0037] The above configuration sets the service shutdown mode to graceful shutdown and sets the shutdown waiting time to N seconds. N is a specific value, such as 5, 8, 10, 11, 15, 20, etc.
[0038] S2: Inside the Spring Boot service, set all asynchronous thread pools to wait until the task is completed before terminating the thread pool, and set the waiting timeout to N seconds to avoid infinite waiting; at the same time, listen to the service shutdown notification, and call the thread pool or thread shutdown method after receiving the notification;
[0039] S3: Create a service file and configure it according to the CentOS service file specification; the service file has the suffix .service and contains the following configuration:
[0040] [Unit]
[0041] Description = MyService
[0042] After=syslog.target network-online.target remote-fs.target nss-lookup.target
[0043] [Service]
[0044] Type=forking
[0045] ExecStart= / opt / java / bin / xxx.sh
[0046] ExecStop= / bin / kill-15$MAINPID
[0047] RemainAfterExit=yes
[0048] StandardOutput = journal
[0049] Restart=always
[0050] RestartSec=N
[0051] [Install]
[0052] WantedBy=multi-user.target;
[0053] ExecStart refers to the path of the web service startup script, Restart=always means the service is always restarted, RestartSec=Ns means the service will restart after N seconds, and ExecStop= / bin / kill-15$MAINPID means the service uses kill-15 to stop the process. xxx is the name of the service file.
[0054] S4: Place the service file in the system path / lib / systemd / system / ; and check whether the service is successfully started by executing the systemctl start xxx command, where xxx is the name of the service file.
[0055] In this embodiment, the Web service is made into a system service and included in the monitoring scope of the system; the Web service is operated through simple and convenient system commands (such as systemctl restart, stop, etc.) to realize the automatic restart function.
[0056] In this embodiment, a SIGTERM signal is sent to the process to request it to exit gracefully, so as to ensure data consistency and integrity of resource release.
[0057] This embodiment is based on the automatic restart technology of CentOS 7.0 and is used for the reliable operation of Web services.
[0058] In summary, the web service of the present invention is based on the method of automatic restart of centos, and the running process is gracefully terminated by using the kill-15 command. By sending a SIGTERM signal to the process, it is requested to exit gracefully to ensure data consistency and integrity of resource release. In addition, by making the Web service as a system service and incorporating it into the monitoring scope of the system. In this way, users can operate the Web service through simple and convenient system commands (such as systemctl restart, stop, etc.) to achieve the automatic restart function. The present invention significantly improves the stability and manageability of Web services, and provides users with a more convenient and efficient operating experience.
[0059] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for automatically restarting a web service based on centos, characterized in that: The following steps are involved: S1: In Java services based on Spring Boot 2.3.0 and above, add the following configuration to the application.properties configuration file to achieve graceful shutdown of the service: server.shutdown=graceful spring.lifecycle.timeout-per-shutdown-phase=Ns; The above configuration sets the service shutdown mode to graceful shutdown and sets the shutdown wait time to N seconds; S2: Inside the Spring Boot service, set all asynchronous thread pools to wait until the task is completed before terminating the thread pool, and set the waiting timeout to N seconds to avoid infinite waiting; at the same time, listen to the service shutdown notification, and call the thread pool or thread shutdown method after receiving the notification; S3: Create a service file and configure it according to the CentOS service file specification; the service file has the suffix .service and contains the following configuration: [Unit] Description = MyService After=syslog.target network-online.target remote-fs.target nss-lookup.target[Service] Type=forking ExecStart= / opt / java / bin / xxx.sh ExecStop= / bin / kill-15$MAINPID RemainAfterExit=yes StandardOutput = journal Restart=always RestartSec=N [Install] WantedBy=multi-user.target; ExecStart refers to the path of the web service startup script, Restart=always means the service is always restarted, RestartSec=Ns means the service restarts after N seconds, ExecStop= / bin / kill-15$MAINPID means the service stops the process using kill-15; xxx is the name of the service file; S4: Place the service file in the system path / lib / systemd / system / ; And check whether the service is started successfully by executing the systemctlstart xxx command, where xxx is the name of the service file.
2. According to claim 1, a method for automatically restarting a web service based on centos is characterized in that: Make the Web service a system service and include it in the system's monitoring scope; operate the Web service through simple and convenient system commands to achieve automatic restart function.
3. According to claim 1, a method for automatically restarting a web service based on centos is characterized in that: By sending a SIGTERM signal to the process, it is requested to exit gracefully to ensure data consistency and integrity of resource release.
4. According to claim 1, a method for automatically restarting a web service based on centos is characterized in that: Automatic restart technology based on CentOS 7.0 for reliable operation of Web services.
Citation Information
Patent Citations
Method for effectively solving graceful start and stop of containerized application
CN117076047A