一、实现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.jarif [ "$1" = "start" ]; then# 如果jar_pid文件存在if [ -f "$jar_pid" ]; then# 如果jar_pid文件有值if [ -s "$jar_pid" ]; thenecho "Existing PID file found during start."# 如果jar_pid文件可读if [ -r "$jar_pid" ]; thenPID=`cat "$jar_pid"`# 与直接执行命令相比, 这样可以抑制输出ps -p $PID >/dev/null 2>&1# 等于0if [ $? -eq 0 ] ; thenecho "$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 $PIDexit 1elseecho "Removing/clearing stale PID file."# 与直接执行命令相比, 这样可以抑制输出rm -f "$jar_pid" >/dev/null 2>&1if [ $? != 0 ]; then# 可写权限if [ -w "$jar_pid" ]; thencat /dev/null > "$jar_pid"elseecho "Unable to remove or clear stale PID file. Start aborted."exit 1fififielseecho "Unable to read PID file. Start aborted."exit 1fielserm -f "$jar_pid" >/dev/null 2>&1if [ $? != 0 ]; thenif [ ! -w "$jar_pid" ]; thenecho "Unable to remove or write to empty PID file. Start aborted."exit 1fifififi# 将命令行参数往前移一个. 比如sh test.sh 1 2, 在shift前, $1=1 $2=2, 在shift后, $1=2shift# eval是将字符串解析为命令执行,如 eval "ls -l"就相当于直接运行性ls -leval "nohup java -jar \$jar >/dev/null 2>&1 &"# 将pid写入到jar_pid文件中echo $! > "$jar_pid"echo "$jar started."
elif [ "$1" = "stop" ]; thensleep=5# 当force为1时, 执行kill -9force=0shift# 若文件存在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 ]; thenecho "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 1fielseecho "PID file is empty and has been ignored."fielseecho "$jar_pid was set but the specified file does not exist. Is $jar running? Stop aborted."exit 1fi# 与直接kill -15相比, 这样可以抑制输出kill -15 `cat "$jar_pid"` >/dev/null 2>&1if [ -f "$jar_pid" ]; thenwhile [ $sleep -ge 0 ]; do# kill -0不影响进程执行,而是检查进程是否正在运行kill -0 `cat "$jar_pid"` >/dev/null 2>&1# 如果大于0表示异常, 表示进程已被关闭if [ $? -gt 0 ]; thenrm -f "$jar_pid" >/dev/null 2>&1# 如果删除失败if [ $? != 0 ]; thenif [ -w "$jar_pid" ]; thencat /dev/null > "$jar_pid"force=0elseecho "The PID file could not be removed or cleared."fifiecho "$jar stopped."breakfiif [ $sleep -gt 0 ]; thensleep 1fiif [ $sleep -eq 0 ]; thenecho "$jar did not stop in time."if [ $force -eq 0 ]; thenecho "PID file was not removed."fiecho "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`donefiKILL_SLEEP_INTERVAL=5if [ $force -eq 1 ]; thenif [ -f "$jar_pid" ]; thenPID=`cat "$jar_pid"`echo "Killing $jar with the PID: $PID"kill -9 $PIDwhile [ $KILL_SLEEP_INTERVAL -ge 0 ]; dokill -0 `cat "$jar_pid"` >/dev/null 2>&1if [ $? -gt 0 ]; thenrm -f "$jar_pid" >/dev/null 2>&1if [ $? != 0 ]; thenif [ -w "$jar_pid" ]; thencat /dev/null > "$jar_pid"elseecho "The PID file could not be removed."fifiecho "The $jar process has been killed."breakfiif [ $KILL_SLEEP_INTERVAL -gt 0 ]; thensleep 1fiKILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 `doneif [ $KILL_SLEEP_INTERVAL -lt 0 ]; thenecho "$jar has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."fififi
elseecho "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