Rocky (centos) jar is registered as a service, which can start automatically at boot

overview

Involved: 1) sh cannot run java command directly, it can be connected softly, here is the direct path

2) The sh script reports a bunch of spaces and newline errors: it needs to be converted into the unix standard format;

#切换到上传的脚本路径
dos2unix 脚本文件名.sh

2) SELINUX authorization allows launching sh scripts

3) SELINUX authorization allows start-up to write log files

Soft link method (no action required)

cd /usr/bin
ln -s -f /usr/local/jdk17/java

Steps

1. Create a new sh script

 New: service_script.sh script

Note: Need to configure
1) jdk path; 2) program log path; 3) java name; 4) start port

#!/bin/bash 
#这里替换为jar包名字
APP_NAME=hy-admin.jar
#根据实际情况修改参数
JVM="-server -Xms4g -Xmx4g"
#APPFILE_PATH="-Dspring.config.location=/usr/local/config/application.properties"

export JAVA_HOME=/usr/local/jdk17
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin


#使用说明,用来提示输入参数 
usage() { 
    echo "Usage: sh 执行脚本.sh [start|stop|restart|status|log|backup] [port]" 
    exit 1 
} 

port=$2 
#检查指定端口是否是有程序在运行
is_exist(){
    # 默认端口
    if [ "${port}" == "" ]; then
    # 默认使用9000
	port=9000
    fi 
    # 获取端口占用的进程号
    pid=`lsof -i:$port | awk 'NR == 2' | awk '{print  $2}'`
    #如果不存在返回1,存在返回0
    if [ -z "${pid}" ]; then 
        return 1 
    else 
        return 0 
    fi 
} 

#启动方法 
start(){ 
    is_exist 
    if [ $? -eq "0" ]; then 
        echo "${port} 端口已经被 ${pid} 进程占用" 
    else 
        #nohup java $JVM -jar $APPFILE_PATH $APP_NAME > /dev/null 2>&1 
                #后台启动jar包,且控制环境变量,根据实际情况修改吧。
        #nohup java $JVM -jar $APP_NAME --spring.profiles.active=prod > /dev/null 2>&1 &
	cd /data/hy-cost-grain-pro
	echo "">log.file
	nohup ${JAVA_HOME}/bin/java -jar ${APP_NAME} --server.port=${port} >log.file  2>&1 &
	echo " ${APP_NAME} 进程 $! 在 ${port} 端口启动中......"
	log
    fi
} 

#停止方法 
stop(){ 
    is_exist 
    if [ $? -eq "0" ]; then 
        kill -9 $pid
	echo "${port} 端口的进程 ${pid} 已经被中断" 
    else 
        echo "${port} 端口未启用" 
    fi 
} 

#输出运行状态 
status(){ 
    is_exist 
    if [ $? -eq "0" ]; then 
        echo "${port} 端口已被进程  ${pid} 占用" 
    else 
        echo "${port} 端口未被占用" 
    fi 
} 
#重启 
restart(){ 
    stop 
    start 
} 

#日志
log(){
        # 输出实时日志
    tail -n 100 -f log.file
}

#备份
backup(){
        #根据需求自定义备份文件路径。
    BACKUP_PATH=./back/
        #获取当前时间作为备份文件名
    BACKUP_DATE=`date +"%Y%m%d(%H:%M:%S)"`
    echo 'backup file ->'$BACKUP_PATH$BACKUP_DATE'.jar'
        #备份当前jar包
    cp -r ./$APP_NAME  $BACKUP_PATH$BACKUP_DATE'.jar'
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明 
case "$1" in 
    "start") 
        start 
        ;; 
    "stop") 
        stop 
        ;; 
    "status") 
        status 
        ;; 
    "restart") 
        restart 
        ;; 
    "log") 
        log 
        ;; 
    "backup") 
        backup 
        ;; 
    *) 
usage 
;; 
esac

Note that the uploaded .sh script is converted into a file in unix encoding format that the system can recognize

Switch to the script path after uploading: run the following command

dos2unix service_script.sh

2. Generate log files and authorize

SELINUX allows to start the file written to the log; 2-3 steps, the theory can also close SELINUX

echo ''>/data/hy-cost-grain-pro/log.file
chmod 777 /data/hy-cost-grain-pro/log.file

#授予启动脚本写入
semanage fcontext -a -t var_log_t /data/hy-cost-grain-pro/log.file
restorecon -Rv /data/hy-cost-grain-pro/log.file

#查看授权
semanage fcontext -l|grep var_log_t

3. Authorize startup script startup permission

Set SELINUX to allow the steps to start and run, 2-3 steps,

#允许运行启动脚本
semanage fcontext -a -t init_exec_t /data/hy-cost-grain-pro/service_script.sh
restorecon -R -v /data/hy-cost-grain-pro/service_script.sh

#查看授权
semanage fcontext -l|grep init_exec_t

Or close selinux (see below) and authorize

chmod 777 /data/hy-cost-grain-pro/service_script.sh

4. Create a new startup service file

Create a new java-grain-9000.service file

[Unit]
Description=java-cost-grain-pro
After=syslog.target
[Service]
Type=simple
#延迟启动,如果jar包需要运用到数据库,所以在这加了睡眠10秒
ExecStartPre=/bin/sleep 8 
ExecStart=/data/hy-cost-grain-pro/service_script.sh start
ExecReload=/data/hy-cost-grain-pro/service_script.sh restart
ExecStop=/data/hy-cost-grain-pro/service_script.sh stop

[Install]
WantedBy=multi-user.target

5. Upload and add automatic startup

Upload to: /etc/systemd/system

#刷新服务配置
systemctl daemon-reload
# 启动服务
systemctl start java-grain-9000.service
# 服务开机自启
systemctl enable java-grain-9000.service
# 停止服务
systemctl stop java-grain-9000.service
# 查看服务状态
systemctl status java-grain-9000.service -l

Reboot, test boot self-start, it took 15s to start

reboot

 

 Possible problems:

1) None: lsof command

yum install lsof

 2) Introduction to SELinux

SELinux has three working modes, note: there is no special security requirement, it can be permissive; different levels have the same configuration;

  1. enforcing: enforcement mode. Any behavior that violates the policy will be prohibited and a warning message will be generated.

  2. permissive: Permissive mode. Violations of the policy will not be banned, only warning messages will be generated.

  3. disabled: Turn off SELinux.

3) selinux is permanently closed

1. Temporary closure

setenforce 0

Note: It will be turned on after restarting the system.

2. Close permanently
1. vim /etc/selinux/config, SELINUX=disabled, then save and exit.

2. vim /etc/sysconfig/selinux, SELINUX=disabled, then save and exit.

Note: If it does not take effect, restart 

3. Verification method
Input command: getenforce  

4) If the database of the switching path can also be started automatically: please refer to my blog

Centos(rocky, red Hat) yum install mysql, switch path and set boot automatically

Centos (rocky, red Hat) yum install mysql, switch path and set boot self- start Simple way 2) rpm package installation, remember to download the stable source package of the corresponding system, which is relatively simple 3) compressed package installation: high degree of freedom, but the configuration is correspondingly complicated, and you need to register the self-starting service Note: 1, 2, 3 cannot coexist on one machine, 1, 2 can start multiple services through mulit-mysql, but it can’t be realized: the myql master-slave on a single machine only has 3 ways to support one machine to install multiple mysql services, and realize the mysql master-slave one , Installation steps 1. Get the latest yum rmp package address download address: MySQL :: MySQL Commu_rocky and red hat https://blog.csdn.net/qq_26408545/article/details/124114793?csdn_share_tail=%7B%22type%22% 3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22124114793%22%2C%22source%22%3A%22qq_26408545%22%7D

Guess you like

Origin blog.csdn.net/qq_26408545/article/details/132071480