How to set up jar application startup on CentOS7

How to set up jar application startup on CentOS7

Pitfalls encountered during project deployment

When deploying Zhihudemo, I did not use automated deployment tools such as Jenkins. I encountered a pit when deploying the jar package project in Linux CentOS. When I remotely connected to the server through xshell and used the "java -jar" command in the system to start the jar package, the output log would occupy the current terminal page. If I closed the terminal window, the entire Java program would stop. To solve this problem, I could set the jar package as a background service, and further set it as a system service and boot automatically.

1. Write a startup script

The script can be stored anywhere. In this example, it is stored in /etc/rc.d/init.d/file-preview-server.sh

# !/bin/bash
# program
# file preview server start 

export JAVA_HOME=/opt/jdk8
export JRE=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE/lib:.
export PATH=$JAVA_HOME/bin/:$JRE/bin:$PATH
nohup java -jar /root/file-preview-server/file-preview-server.jar > /root/file-preview-server/stdout.log &

2. Add execution permissions to the /etc/rc.d/init.d/file-preview-server.sh file

chmod +x /etc/rc.d/init.d/file-preview-server.sh

3. Add execution permissions to the /etc/rc.d/rc.local file

chmod +x /etc/rc.d/rc.local

4. Edit /etc/rc.d/rc.local file

Add the following statement at the end of the file

su - root -c '/etc/rc.d/init.d/file-preview-server.sh'

Now, you’re done!

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to detect Java installation and run jar application in CentOS system
  • The best way to start a jar package project under Centos7 server

<<:  About the bug of better-scroll plug-in that cannot slide (solved by plug-in in 2021)

>>:  MYSQL unlock and lock table introduction

Recommend

Detailed analysis of the MySQL slow log opening method and storage format

In development projects, we can monitor SQL with ...

Detailed explanation of nginx installation, deployment and usage on Linux

Table of contents 1. Download 2. Deployment 3. Ng...

Example code for achieving hollowing effect with pure CSS

I have recently studied the hollowing effect. bac...

Docker deployment of Flask application implementation steps

1. Purpose Write a Flask application locally, pac...

Web Design Experience: Efficiently Writing Web Code

Originally, this seventh chapter should be a deep ...

How to view the network routing table in Ubuntu

What are Routing and Routing Table in Linux? The ...

Advanced crawler - Use of Scrapy_splash component for JS automatic rendering

Table of contents 1. What is scrapy_splash? 2. Th...

Vue implements simple slider verification

This article example shares the implementation of...

How to detect whether a file is damaged using Apache Tika

Apache Tika is a library for file type detection ...

Form submission page refresh does not jump

1. Design source code Copy code The code is as fol...

Learn v-model and its modifiers in one article

Table of contents Preface Modifiers of v-model: l...

How to solve the margin collapse problem in CSS

First, let's look at three situations where m...

Learn MySQL in a simple way

Preface The database has always been my weak poin...