SpringBoot's learning has finally come to an end, but it still leaves us with a question, that is, how should the program be deployed on the Linux server? Will the program be shutdown if ssh is disconnected after starting the program with the command? The first problem is easier to solve. You only need to ensure that the jdk is installed correctly and there will be no big problems. The tricky problem is the latter. The key to solving the problem is to ensure that the program runs in a daemon process (background). This time I will share how to correctly deploy programs under Linux
In order to solve this problem, I checked the information on the Internet and found the following 3 solutions
1 Package jar into system service (not recommended)
The disadvantage of this method is also quite obvious, that is, the service is prone to not being started, because different Linux distributions have more or less differences to the same extent.
2 Use nohup command to cooperate with kill -9 xxx
This method is easier to accept and is also a compromise, but it will be more troublesome to stop the program and use the command to find the process and then use kill -9.
3 Use screen, tmux, etc. (recommended)
Considering this method comprehensively, it is more scientific because it provides a unified interface and corresponding functions to manage multiple sessions, which is easier to operate. The disadvantage is that it requires a little learning cost.
Commonly used tmux commands:
$ tmux new -s session-name Create new session
$ tmux a -t session-name Access to the specified session
$ tmux detach /$ Ctrl-b d Disconnect the current session
$ tmux kill-session -t session-name Close the session
$ tmux ls Show all sessions
Common screen commands:
$ screen -S window-name Create a new session
$screen -r Session number or name to access the specified session/reconnect session
$screen -ls Show all sessions
Ctrl+ab Separate the current session
Ctrl+ak
The following are their differences. Although tmux is more advanced, how to choose depends on everyone's usage habits
tmux is very similar to screen, but better than screen. To ask where the good things are, the simple answer is that although the functions of screen are the same, tmux is designed better. screen is available, but not stable.
Here are some places where tmux goes beyond screen:
SpringBoot is more convenient to use maven to package. Check the configuration of pom.xml when packaging to ensure that there are the following configurations:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork : Without this configuration, devtools will not work, that is, the application will not restart --> <fork>true</fork> <!-- Optional configuration --> <executable>true</executable> </configuration> </plugin> </plugins> lt;/build>
Just use maven install for packaging. If the package successfully finds the corresponding jar and renames it, upload it to the linux server for authorization.
This way the application is started, even if the command line is closed or disconnected from ssh, it will not affect the normal operation of the program.
Disconnect
After the session is finished, the program is shutdown, as shown below
The use of screen is similar to tmux. It has been practiced here, so I deploy the program to run in the background. The effect of selecting screen and tmux is similar.
PS: Here is a description of the deployment of spring-boot in Linux
First, maven clean the project, then maven install it into a jar package on the project, and then delete the previous version of the process on Linux.
Check the command ps -ef | grep java
Kill process kill -9 process number
Run the spring-boot project directly nohup java -jar sinocube.jar & Use nohup to end with &
nohup returns to output log files. The log file can customize the file name, default is nohup.nohup.out
cat nohup.out View log nohup.out is the file name tailf nohup.out View log in real time