需求:制作一个可选择的jar包重启脚本,由于我这边的jar包在docker内,所以有些复杂,仅做参考
#!/bin/bash source ~/.bashrc echo "----------------------------------" echo -e "\033[35m JAR包名称序号\033[0m" echo "(1) gateway-two.jar" echo "(0) Exit Menu" echo "----------------------------------" read -p "请输入要重新启动的JAR包序列号:" input case $input in 1) echo "开始启动gateway-two.jar" sleep 1 jar=gateway-two docker exec -it jar bash -c "sed -i '/jar/d' /root/.bashrc" #删除/root/.bashrc文件的最后一行,以防下面叠加echo时不断增大 docker exec -it jar bash -c "echo 'export jar=$jar' >> /root/.bashrc" #把变量从宿主机外部传进docker容器内部 docker exec -i jar bash<<'EOF' source /root/.bashrc #使之前传进来的变量生效,因为下面会用到 pid=`ps -ef|grep ${jar}.jar|grep -v grep|awk '{print $2}'` #获取jar程序PID号 cd /vrgv/jar/2.0/$jar/ if [ -n "${pid}" ];then kill -9 ${pid} echo "Process $jar killed!" nohup java -jar -Xms265M -Xmx512M ${jar}.jar > /vrgv/jar/2.0/logs/${jar}.log 2>&1 & echo "ReStart $jar Success!" else nohup java -jar -Xms265M -Xmx512M ${jar}.jar > /vrgv/jar/2.0/logs/${jar}.log 2>&1 & echo "Start $jar Success!" fi exit EOF ;; 0) exit;; esac
执行流程:
1.输入jar包序列号
2.传变量进docker容器内部
3.判断jar程序是否存在,存在就杀死重新启动,不存在则直接启动
效果图: