We know that starting springboot project in three ways:
- Run the main method to start
- Use the command mvn spring-boot: run "start the application from the command line
- Run "mvn package" when packaged, will be packaged into a JAR file can be run directly using "java -jar" command can be run directly.
When we developed, often we use the first two, and when deployed will often use a third. However, when we use the java -jar to run, not run in the background.
Below us, how to say back to start springboot project on the server. In fact, there are many methods to achieve here is listed two kinds of relatively easy ways:
nohup and Shell
This method is mainly achieved by using nohup command details of this command is as follows:
nohup command
Use: Do not hang up the run command.
Syntax: nohup Command [Arg ...] [&]
Description: nohup command to run specified by the Command parameter and any related Arg parameters of command, ignore all hang (SIGHUP) signals. The program runs in the background using nohup command after logout. To run a nohup command in the background, add & to the end of the command.
Example:
1
|
nohup
java -jar xxx.jar &
|
After such execution, execution nohup will result in the log output to the current folder below nohup.out file, usually we can use the above command .
We can also manually specify a parameter to specify the log file output location, such as:
1
|
nohup
java -jar xxx.jar > catalina.out 2>&1 &
|
If no log output, the following command can be used
1
|
nohup
java -jar xxx.jar >
/dev/null
&
|
So, we only need to use nohup java -jar yourapp.jar & command, you can let yourapp.jar running in the background. However, in order to facilitate the management, we can also write some scripts for starting an application by Shell, such as following a few:
- Close the application scripts: stop.sh
123456789
#!/bin/bash
PID=$(
ps
-ef |
grep
yourapp.jar |
grep
-
v
grep
|
awk
'{ print $2 }'
)
if
[ -z
"$PID"
]
then
echo
Application is already stopped
else
echo
kill
$PID
kill
$PID
fi
- Script to launch an application: start.sh
12
#!/bin/bash
nohup
java -jar yourapp.jar --server.port=8888 &
- Integrated script shutdown and startup of: run.sh, because the application will first perform a shutdown, and then start the application, so as not to cause port conflicts and other issues, repeated calls for continuous integration system.
12345
#!/bin/bash
echo
stop application
source
stop.sh
echo
start application
source
start.sh
system service
In Maven plug-in Spring Boot, also provides the ability to build a complete executable program, what does that mean? That is, we can not java -jar, but directly run the jar to execute the program. So that we can easily be created as a system service running in the background. The main steps are as follows:
- Add Spring Boot plug-in pom.xml, and pay attention to set the executable configuration
1234567891011
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot<
/groupId
>
<artifactId>spring-boot-maven-plugin<
/artifactId
>
<configuration>
<executable>
true
<
/executable
>
<
/configuration
>
<
/plugin
>
<
/plugins
>
<
/build
>
-
After completion of the above-described configuration, use mvn install packaged, package build an executable jar
-
Create a soft link to the directory under /etc/init.d/
1sudo
ln
-s
/var/yourapp/yourapp
.jar
/etc/init
.d
/yourapp
- After completing the soft connection is created, we can apply it to yourapp.jar controlled by the following command to start, stop, restart the operation
1
/etc/init
.d
/yourapp
start|stop|restart
To sum up, in general, we use the following command:
1
|
nohup java -jar xxx.jar &
|
We know that starting springboot project in three ways:
- Run the main method to start
- Use the command mvn spring-boot: run "start the application from the command line
- Run "mvn package" when packaged, will be packaged into a JAR file can be run directly using "java -jar" command can be run directly.
When we developed, often we use the first two, and when deployed will often use a third. However, when we use the java -jar to run, not run in the background.
Below us, how to say back to start springboot project on the server. In fact, there are many methods to achieve here is listed two kinds of relatively easy ways:
nohup and Shell
This method is mainly achieved by using nohup command details of this command is as follows:
nohup command
Use: Do not hang up the run command.
Syntax: nohup Command [Arg ...] [&]
Description: nohup command to run specified by the Command parameter and any related Arg parameters of command, ignore all hang (SIGHUP) signals. The program runs in the background using nohup command after logout. To run a nohup command in the background, add & to the end of the command.
Example:
1
|
nohup
java -jar xxx.jar &
|
After such execution, execution nohup will result in the log output to the current folder below nohup.out file, usually we can use the above command .
We can also manually specify a parameter to specify the log file output location, such as:
1
|
nohup
java -jar xxx.jar > catalina.out 2>&1 &
|
If no log output, the following command can be used
1
|
nohup
java -jar xxx.jar >
/dev/null
&
|
So, we only need to use nohup java -jar yourapp.jar & command, you can let yourapp.jar running in the background. However, in order to facilitate the management, we can also write some scripts for starting an application by Shell, such as following a few:
- Close the application scripts: stop.sh
123456789
#!/bin/bash
PID=$(
ps
-ef |
grep
yourapp.jar |
grep
-
v
grep
|
awk
'{ print $2 }'
)
if
[ -z
"$PID"
]
then
echo
Application is already stopped
else
echo
kill
$PID
kill
$PID
fi
- 启动应用的脚本:start.sh
12
#!/bin/bash
nohup
java -jar yourapp.jar --server.port=8888 &
- 整合了关闭和启动的脚本:run.sh,由于会先执行关闭应用,然后再启动应用,这样不会引起端口冲突等问题,适合在持续集成系统中进行反复调用。
12345
#!/bin/bash
echo
stop application
source
stop.sh
echo
start application
source
start.sh
系统服务
在Spring Boot的Maven插件中,还提供了构建完整可执行程序的功能,什么意思呢?就是说,我们可以不用java -jar,而是直接运行jar来执行程序。这样我们就可以方便的将其创建成系统服务在后台运行了。主要步骤如下:
- 在pom.xml中添加Spring Boot的插件,并注意设置executable配置
1234567891011
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot<
/groupId
>
<artifactId>spring-boot-maven-plugin<
/artifactId
>
<configuration>
<executable>
true
<
/executable
>
<
/configuration
>
<
/plugin
>
<
/plugins
>
<
/build
>
-
在完成上述配置后,使用mvn install进行打包,构建一个可执行的jar包
-
创建软连接到/etc/init.d/目录下
1sudo
ln
-s
/var/yourapp/yourapp
.jar
/etc/init
.d
/yourapp
- 在完成软连接创建之后,我们就可以通过如下命令对yourapp.jar应用来控制启动、停止、重启操作了
1
/etc/init
.d
/yourapp
start|stop|restart
综上,一般情况下我们使用以下命令即可:
1
|
nohup java -jar xxx.jar &
|