Run java project/jar package in linux background: nohup command

1.Ask a question

We export a SpringBoot project as a jar package, upload the jar package to the Alibaba Cloud ECS server, and use the java -jar xxx-xxx.jar command to start the SpringBoot program. At this time, our local xshell client must always be open. Once the xshell client is closed, the java -jar xxx-xxx.jar process will be terminated and the SpringBoot program will not be able to access it.

Therefore, we hope that after starting the SpringBoot jar package, the corresponding process can always run and will not be terminated because the xshell client is closed.

image-20230718012521309

2.Solution

2.1 Foreground and background operation

By default, Linux commands are run in the foreground. The characteristic of foreground running is that until the previous command is executed, the command line will always be occupied by the previous command, and no new commands can be entered or executed.

But running in the background does not solve the problem raised above: after our shell client (for example: xshell) disconnects from the server, the SpringBoot process will end, which obviously does not meet our original intention of deploying and running the project.

2.2 Run without hanging up

The so-called "not hanging up" means that after the client disconnects, the process started by the command is still running. nohupThe command is short for "no hang up". The complete way to use the nohup command to start the SpringBoot microservice project is:

nohup java -jar spring-boot-demo.jar>springboot.log 2>&1 &

image-20230718012741721

Guess you like

Origin blog.csdn.net/qq_62982856/article/details/133770736