jar packages using springboot can be started in service mode
- 2021-11-29 23:42:34
- OfStack
The jar packet of springboot is started in service mode
Scene
The typed jar package with java-jar can definitely be started. This method is native and simple, but it is not friendly to operation and maintenance.
Therefore, it is required to transform, hoping to start it with service command.
Process
It is technically feasible.
pom. xml configuration
There are two configuration points in pom. xml:
<finalName>custom-app</finalName>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- Executable -->
<executable>true</executable>
</configuration>
</plugin>
Note: finalName should be uniqueness, and should not carry a version number such as. 1.1. SNAPSHOT.
Packaging (maven), Authorization, Startup
Package first, and then execute the following script:
# Authorization
chmod 755 custom-app ;
# Start
./custom-app.jar
If it can be executed, it means that the maven configuration is in effect and the jar package is an execution file.
Note: Looking at the jar package, you can see that the first 2,300 lines include the shell script, which is < executable > true < /executable > The generated content.
Another: java-jar is still usable and will not be affected.
Establish a soft connection and start it through service command
The order is as follows:
# Establish a soft connection
ln -s /data/custom-app.jar /etc/init.d/custom-app
# Then you can use service Command started
service custom-app start
It is found that the log is not output, so why is it started? How to read the journal?
# You can see the startup log here
/var/log/custom-app.log
# View pid, The template is: /var/run/<appname>/<appname>.pid
/var/run/custom-app/custom-app.pid
systemctl Configuration
Because it is useless, it is temporarily omitted.
The usage of systemctl configuration is also found in the bottom spring document.
Others
Official website document about configuration
Springboot starts, closes, and restarts scripts as jar packages
Start
Write startup scripts startup.sh
#!/bin/bash
echo Starting application
nohup java -jar activiti_demo-0.0.1-SNAPSHOT.jar &
Authorization
chmod +x startup.sh
Shut down
Write a shutdown script stop.sh
#!/bin/bash
PID=$(ps -ef | grep activiti_demo-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo Application is already stopped
else
echo kill $PID
kill $PID
fi
Authorization
chmod +x stop.sh
Restart
Write a restart script restart.sh
#!/bin/bash
echo Stopping application
source ./stop.sh
echo Starting application
source ./startup.sh
Authorization
chmod +x restart.sh