Linux配置JAR包为服务实现自启动

news/2024/7/24 3:49:06 标签: linux, jar, 运维

一、实现bash脚本

1.1 绘图工具

绘图需安装idea的插件plantUML-Integration

只需要上图一个就可以,别的也不需要装。

启动服务的逻辑如下

关闭服务的逻辑如下

1.2 逻辑实现

在/root路径下创建entrance文件,实现逻辑如下

#!/usr/bin/env bash
# 2>&1的含义
# 1表示标准输出
# 2表示标准错误输出
# 2>&1表示将标准错误输出重定向到标准输出

# 配置jar程序的pid保存文件
jar_pid=/root/jar_pid
# 配置jar的绝对路径
jar=/root/http-proxy-boot.jar

if [ "$1" = "start" ]; then
    # 如果jar_pid文件存在
    if [ -f "$jar_pid" ]; then
        # 如果jar_pid文件有值
        if [ -s "$jar_pid" ]; then
            echo "Existing PID file found during start."
            # 如果jar_pid文件可读
            if [ -r "$jar_pid" ]; then
                PID=`cat "$jar_pid"`
                # 与直接执行命令相比, 这样可以抑制输出
                ps -p $PID >/dev/null 2>&1
                # 等于0
                if [ $? -eq 0 ] ; then
                    echo "$jar appears to still be running with PID $PID. Start aborted."
                    echo "If the following process is not a $jar process, remove the PID file and try again:"
                    ps -f -p $PID
                    exit 1
                else
                    echo "Removing/clearing stale PID file."
                    # 与直接执行命令相比, 这样可以抑制输出
                    rm -f "$jar_pid" >/dev/null 2>&1
                    if [ $? != 0 ]; then
                        # 可写权限
                        if [ -w "$jar_pid" ]; then
                            cat /dev/null > "$jar_pid"
                        else
                            echo "Unable to remove or clear stale PID file. Start aborted."
                            exit 1
                        fi
                    fi
                fi
            else
                echo "Unable to read PID file. Start aborted."
                exit 1
            fi
        else
            rm -f "$jar_pid" >/dev/null 2>&1
            if [ $? != 0 ]; then
                if [ ! -w "$jar_pid" ]; then
                    echo "Unable to remove or write to empty PID file. Start aborted."
                    exit 1
                fi
            fi
        fi
    fi
    # 将命令行参数往前移一个. 比如sh test.sh 1 2, 在shift前, $1=1 $2=2, 在shift后, $1=2
    shift
    # eval是将字符串解析为命令执行,如 eval "ls -l"就相当于直接运行性ls -l
    eval "nohup java -jar \$jar >/dev/null 2>&1 &"
    # 将pid写入到jar_pid文件中
    echo $! > "$jar_pid"
    echo "$jar started."
elif [ "$1" = "stop" ]; then
    sleep=5
    # 当force为1时, 执行kill -9
    force=0
    shift
    # 若文件存在
    if [ -f "$jar_pid" ]; then
        # 若jar_pid有值
        if [ -s "$jar_pid" ]; then
            # kill -0不影响进程执行,而是检查进程是否正在运行
            kill -0 `cat "$jar_pid"` >/dev/null 2>&1
            # 如果大于0表示异常
            if [ $? -gt 0 ]; then
              echo "PID file found but either no matching process was found or the current user does not have permission to stop the process. Stop aborted."
              exit 1
            fi
        else
            echo "PID file is empty and has been ignored."
        fi
    else
      echo "$jar_pid was set but the specified file does not exist. Is $jar running? Stop aborted."
      exit 1
    fi
    # 与直接kill -15相比, 这样可以抑制输出
    kill -15 `cat "$jar_pid"` >/dev/null 2>&1
    if [ -f "$jar_pid" ]; then
        while [ $sleep -ge 0 ]; do
            # kill -0不影响进程执行,而是检查进程是否正在运行
            kill -0 `cat "$jar_pid"` >/dev/null 2>&1
            # 如果大于0表示异常, 表示进程已被关闭
            if [ $? -gt 0 ]; then
                rm -f "$jar_pid" >/dev/null 2>&1
                # 如果删除失败
                if [ $? != 0 ]; then
                    if [ -w "$jar_pid" ]; then
                        cat /dev/null > "$jar_pid"
                        force=0
                    else
                        echo "The PID file could not be removed or cleared."
                    fi
                fi
                echo "$jar stopped."
                break
            fi
            if [ $sleep -gt 0 ]; then
                sleep 1
            fi
            if [ $sleep -eq 0 ]; then
              echo "$jar did not stop in time."
              if [ $force -eq 0 ]; then
                echo "PID file was not removed."
              fi
              echo "To aid diagnostics a thread dump has been written to standard out."
              # kill -3 与 kill -15 类似, 只是kill -3 会多了一步生成核心存储,用于后续调试。kill -3适用于程序无响应时
              kill -3 `cat "$jar_pid"`
            fi
            # 自减
            sleep=`expr $sleep - 1`
        done
    fi
    KILL_SLEEP_INTERVAL=5
    if [ $force -eq 1 ]; then
        if [ -f "$jar_pid" ]; then
            PID=`cat "$jar_pid"`
            echo "Killing $jar with the PID: $PID"
            kill -9 $PID
            while [ $KILL_SLEEP_INTERVAL -ge 0 ]; do
                kill -0 `cat "$jar_pid"` >/dev/null 2>&1
                if [ $? -gt 0 ]; then
                    rm -f "$jar_pid" >/dev/null 2>&1
                    if [ $? != 0 ]; then
                        if [ -w "$jar_pid" ]; then
                            cat /dev/null > "$jar_pid"
                        else
                            echo "The PID file could not be removed."
                        fi
                    fi
                    echo "The $jar process has been killed."
                    break
                fi
                if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then
                    sleep 1
                fi
                KILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 `
            done
            if [ $KILL_SLEEP_INTERVAL -lt 0 ]; then
                echo "$jar has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
            fi
        fi
    fi
else
    echo "commands:"
    echo " start      Start jar"
    echo " stop       Stop jar"
fi

启动和关闭命令如下

sh /root/entrance start
sh /root/entrance stop

二、配置systemd服务

使用systemd的好处时,他由系统管理,统一的管理命令,而且可以支持自启动等操作。

延用上面的bash脚本,创建systemd服务

cat > /usr/lib/systemd/system/http-proxy-boot.service <<EOF
[Unit]
Description=http-proxy-boot
After=network.target
[Service]
Type=forking
# restart时, 先执行ExecStop, 再执行ExecStart
ExecStart=/root/entrance start
ExecStop=/root/entrance stop
PrivateTmp=true
# kill按理说,应该返回状态0,但是java比较特殊,返回的是143
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
EOF

之后就可以进行操作啦

systemctl start|stop|restart|enable|disable http-proxy-boot

三、参考致谢

如何在统信UOS系统中设置tomcat开机启动_统信uos系统部署tomcat-CSDN博客

tomcat/bin/catalina.sh at main · apache/tomcat


http://www.niftyadmin.cn/n/5322956.html

相关文章

Windows python下载

1、下载 进入到地址&#xff1a;https://www.python.org/dowmloads 可以直接下载最新的版本 或者点击Windows&#xff0c;查看下方更多的版本 点击文档就下载下来啦 2、安装 双击下载的文件&#xff0c;勾选Add python.exe to PATH添加到环境变量中&#xff0c;选择Coutomi…

Docker 镜像的详解及创建(Dockerfile详解)

目录 镜像加载的原理 联合文件系统&#xff08;UnionFS&#xff09; 镜像结构的分层 Dockerfile Dockerfile结构 dockerfile常用命令 Dockerfile 编写规范 docker创建镜像的方法 基于现有镜像创建 示例&#xff1a; 基于本地模版创建 示例 基于Dockerfile 创建 示…

【上分日记】第369场周赛(分类讨论 + 数学 + 前缀和)

文章目录 前言正文1.3000. 对角线最长的矩形的面积2.3001. 捕获黑皇后需要的最少移动次数3.3002. 移除后集合的最多元素数3.3003. 执行操作后的最大分割数量 总结尾序 前言 终于考完试了&#xff0c;考了四天&#xff0c;也耽搁了四天&#xff0c;这就赶紧来补这场周赛的题了&a…

大模型背景下计算机视觉年终思考小结(一)

1. 引言 在过去的十年里&#xff0c;出现了许多涉及计算机视觉的项目&#xff0c;举例如下&#xff1a; 使用射线图像和其他医学图像领域的医学诊断应用使用卫星图像分析建筑物和土地利用率相关应用各种环境下的目标检测和跟踪&#xff0c;如交通流统计、自然环境垃圾检测估计…

【LangChain学习之旅】—(6) 提示工程(下):用思维链和思维树提升模型思考质量

【LangChain学习之旅】—&#xff08;6&#xff09; 提示工程&#xff08;下&#xff09;&#xff1a;用思维链和思维树提升模型思考质量 什么是 Chain of ThoughtFew-Shot CoTZero-Shot CoTChain of Thought 实战CoT 的模板设计程序的完整框架Tree of Thought总结 Reference&a…

Android APP修改为鸿蒙APP需要注意的问题

将Android应用修改为鸿蒙&#xff08;HarmonyOS&#xff09;应用需要注意一些关键问题&#xff0c;以确保应用在新平台上的顺利运行。以下是在修改Android应用为鸿蒙应用时需要考虑的一些重要问题&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c;专业的软…

【软件测试】学习笔记-不同视角的软件性能与性能指标

本篇文章探讨新的测试主题&#xff1a;性能测试&#xff0c;因为性能测试的专业性很强&#xff0c;所以我会以从0到1的入门者视角&#xff0c;系统性地阐述性能测试的方法以及应用领域&#xff0c;用实例去诠释各种性能指标。 本篇文章站在全局的视角&#xff0c;帮你梳理软件性…

半监督学习 - 半监督K均值(Semi-Supervised K-Means)

什么是机器学习 半监督K均值&#xff08;Semi-Supervised K-Means&#xff09;是K均值聚类算法的一种扩展&#xff0c;它结合了有标签数据和无标签数据进行聚类。在传统的K均值算法中&#xff0c;所有数据点都是无标签的&#xff0c;而在半监督K均值中&#xff0c;我们允许一部…