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

Blog    

Recommend

mysql IS NULL using index case explanation

Introduction The use of is null, is not null, and...

Summary of the use of html meta tags (recommended)

Meta tag function The META tag is a key tag in th...

How to use Docker to build OpenLDAP+phpLDAPadmin unified user authentication

1. Background Use LDAP to centrally manage operat...

Implementation of interactive data between QT and javascript

1. Data flows from QT to JS 1. QT calls the JS fu...

Vite introduces the implementation of virtual files

Table of contents background Importing virtual fi...

Detailed explanation of how to adjust Linux command history

The bash history command in Linux system helps to...

Summary and examples of vue3 component communication methods

The communication modes of vue3 components are as...

Vue integrates PDF.js to implement PDF preview and add watermark steps

Table of contents Achieve results Available plugi...

Pitfalls based on MySQL default sorting rules

The default varchar type in MySQL is case insensi...

How to configure Linux to use LDAP user authentication

I am using LDAP user management implemented in Ce...

CocosCreator classic entry project flappybird

Table of contents Development Environment Game en...

Detailed explanation of box-sizing in CSS3 (content-box and border-box)

Box-sizing in CSS3 (content-box and border-box) T...

MySQL 8.0.15 installation graphic tutorial and database basics

MySQL software installation and database basics a...