shell脚本批量启动jar、关闭、重启、检查

本文分享了一种高效管理多个JAR包的方法,通过编写Shell脚本来自动化启动、停止、重启和检查状态,大大简化了操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 我这里有11个jar包,挨个启动和停止太麻烦,写了个程序。

前面的true false是控制哪些jar启动,哪些不启动。

根据端口检测是否存活。

按需要改成自己的即可,代码明了。

使用命令:

## 启动
./start.sh start
## 停止 
./start.sh stop
## 重启
./start.sh restart
## 检查是否存活
./start.sh check

代码详情: 

#!/bin/sh

export ftpschedule_enable=true
export ftpservice_enable=true
export manager_enable=true
export timeservice_enable=true
export apiservice_enable=false
export db2file_enable=false
export file2db_enable=false
export file2hdfs_enable=false
export file2kafka_enable=false
export fpf_enable=true
export ftpdl_enable=true



export ftpschedule=collect-ftpschedule/collect_ftpschedule-1.0-SNAPSHOT.jar
export ftpservice=collect-ftpservice/collect_ftpservice-1.0-SNAPSHOT.jar
export manager=collect-manager/collect_manager-1.0-SNAPSHOT.jar
export timeservice=collect-timeservice/collect_timeservice-1.0-SNAPSHOT.jar
export apiservice=collect-apiservice/collect_apiservice-1.0-SNAPSHOT.jar
export db2file=collect-db2file/collect_stage_db2file-1.0-SNAPSHOT.jar
export file2db=collect-file2db/collect_stage_file2db-1.0-SNAPSHOT.jar
export file2hdfs=collect-file2hdfs/collect_stage_file2hdfs.jar
export file2kafka=collect-file2kafka/collect_stage_file2kafka.jar
export fpf=collect-fpf/fpf-framework/fpf_parser-1.0-SNAPSHOT.jar
export ftpdl=collect-stage-ftpdl/collect_stage_ftpdl-1.0-SNAPSHOT.jar

 
export ftpschedule_port=32002
export ftpservice_port=32003
export manager_port=32004
export timeservice_port=32012
export apiservice_port=32001
export db2file_port=32005
export file2db_port=32006
export file2hdfs_port=32007
export file2kafka_port=32008
export fpf_port=32009
export ftpdl_port=32011




case "$1" in
 
start)
	if($timeservice_enable);then
		## 启动timeservice
		echo "--------timeservice 开始启动--------------"
		nohup java -jar $timeservice >/dev/null 2>&1 &
		timeservice_pid=`lsof -i:$timeservice_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$timeservice_pid" ]
		    do
		      timeservice_pid=`lsof -i:$timeservice_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "timeservice pid is $timeservice_pid" 
		echo "--------timeservice 启动成功--------------"
	fi

	if($manager_enable);then
		## 启动manager
		echo "--------manager 开始启动--------------"
		nohup java -jar $manager >/dev/null 2>&1 &
		manager_pid=`lsof -i:$manager_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$manager_pid" ]
		    do
		      manager_pid=`lsof -i:$manager_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "manager pid is $manager_pid" 
		echo "--------manager 启动成功--------------"
	fi

	if($ftpschedule_enable);then
		## 启动ftpschedule
		echo "--------ftpschedule 开始启动--------------"
		nohup java -jar $ftpschedule >/dev/null 2>&1 &
		ftpschedule_pid=`lsof -i:$ftpschedule_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$ftpschedule_pid" ]
		    do
		      ftpschedule_pid=`lsof -i:$ftpschedule_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "ftpschedule pid is $ftpschedule_pid" 
		echo "--------ftpschedule 启动成功--------------"
	fi



	if($ftpservice_enable);then
		## 启动ftpservice
		echo "--------ftpservice 开始启动--------------"
		nohup java -jar $ftpservice >/dev/null 2>&1 &
		ftpservice_pid=`lsof -i:$ftpservice_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$ftpservice_pid" ]
		    do
		      ftpservice_pid=`lsof -i:$ftpservice_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "ftpservice pid is $ftpservice_pid" 
		echo "--------ftpservice 启动成功--------------"
	fi


	if($apiservice_enable);then
		## 启动apiservice
		echo "--------apiservice 开始启动--------------"
		nohup java -jar $apiservice >/dev/null 2>&1 &
		apiservice_pid=`lsof -i:$apiservice_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$apiservice_pid" ]
		    do
		      apiservice_pid=`lsof -i:$apiservice_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "apiservice pid is $apiservice_pid" 
		echo "--------apiservice 启动成功--------------"
	fi

	if($db2file_enable);then
		## 启动db2file
		echo "--------db2file 开始启动--------------"
		nohup java -jar $db2file >/dev/null 2>&1 &
		db2file_pid=`lsof -i:$db2file_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$db2file_pid" ]
		    do
		      db2file_pid=`lsof -i:$db2file_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "db2file pid is $db2file_pid" 
		echo "--------db2file 启动成功--------------"
	fi

	if($file2db_enable);then
		## 启动file2db
		echo "--------file2db 开始启动--------------"
		nohup java -jar $file2db >/dev/null 2>&1 &
		file2db_pid=`lsof -i:$file2db_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$file2db_pid" ]
		    do
		      file2db_pid=`lsof -i:$file2db_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "file2db pid is $file2db_pid" 
		echo "--------file2db 启动成功--------------"
	fi

	if($file2hdfs_enable);then
		## 启动file2hdfs
		echo "--------file2hdfs 开始启动--------------"
		nohup java -jar $file2hdfs >/dev/null 2>&1 &
		file2hdfs_pid=`lsof -i:$file2hdfs_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$file2hdfs_pid" ]
		    do
		      file2hdfs_pid=`lsof -i:$file2hdfs_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "file2hdfs pid is $file2hdfs_pid" 
		echo "--------file2hdfs 启动成功--------------"
	fi

	if($file2kafka_enable);then
		## 启动file2kafka
		echo "--------file2kafka 开始启动--------------"
		nohup java -jar $file2kafka >/dev/null 2>&1 &
		file2kafka_pid=`lsof -i:$file2kafka_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$file2kafka_pid" ]
		    do
		      file2kafka_pid=`lsof -i:$file2kafka_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "file2kafka pid is $file2kafka_pid" 
		echo "--------file2kafka 启动成功--------------"
	fi

	if($fpf_enable);then
		## 启动fpf
		echo "--------fpf 开始启动--------------"
		nohup java -jar $fpf >/dev/null 2>&1 &
		fpf_pid=`lsof -i:$fpf_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$fpf_pid" ]
		    do
		      fpf_pid=`lsof -i:$fpf_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "fpf pid is $fpf_pid" 
		echo "--------fpf 启动成功--------------"
	fi

	if($ftpdl_enable);then
		## 启动ftpdl
		echo "--------ftpdl 开始启动--------------"
		nohup java -jar $ftpdl >/dev/null 2>&1 &
		ftpservice_pid=`lsof -i:$ftpdl_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$ftpdl_pid" ]
		    do
		      ftpdl_pid=`lsof -i:$ftpdl_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "ftpdl pid is $ftpdl_pid" 
		echo "--------ftpdl 启动成功--------------"
	fi
        echo "===startAll success==="
        ;;
 
 stop)
	## 杀掉timeservice
        P_ID=`ps -ef | grep -w ${timeservice##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===timeservice process not exists or stop success"
        else
            kill -9 $P_ID
            echo "timeservice killed success"
        fi

	## 杀掉manager
	P_ID=`ps -ef | grep -w ${manager##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===manager process not exists or stop success"
        else
            kill -9 $P_ID
            echo "manager killed success"
        fi

	## 杀掉ftpschedule
	P_ID=`ps -ef | grep -w ${ftpschedule##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ftpschedule process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ftpschedule killed success"
        fi

	## 杀掉ftpservice
	P_ID=`ps -ef | grep -w ${ftpservice##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ftpservice process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ftpservice killed success"
        fi

	## 杀掉 apiservice
	P_ID=`ps -ef | grep -w ${apiservice##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "=== apiservice process not exists or stop success"
        else
            kill -9 $P_ID
            echo " apiservice killed success"
        fi

	## 杀掉ftpservice
	P_ID=`ps -ef | grep -w ${db2file##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===db2file process not exists or stop success"
        else
            kill -9 $P_ID
            echo "db2file killed success"
        fi

	## 杀掉ftpservice
	P_ID=`ps -ef | grep -w ${file2db##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===file2db process not exists or stop success"
        else
            kill -9 $P_ID
            echo "file2db killed success"
        fi

	## 杀掉ftpservice
	P_ID=`ps -ef | grep -w ${file2hdfs##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===file2hdfs process not exists or stop success"
        else
            kill -9 $P_ID
            echo "file2hdfs killed success"
        fi

	## 杀掉ftpservice
	P_ID=`ps -ef | grep -w ${file2kafka##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===file2kafka process not exists or stop success"
        else
            kill -9 $P_ID
            echo "file2kafka killed success"
        fi

	## 杀掉ftpservice
	P_ID=`ps -ef | grep -w ${fpf##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===fpf process not exists or stop success"
        else
            kill -9 $P_ID
            echo "fpf killed success"
        fi

	## 杀掉ftpservice
	P_ID=`ps -ef | grep -w ${ftpdl##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ftpdl process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ftpdl killed success"
        fi
 
        echo "===stopAll success==="
        ;;   
 
restart)
        $0 stop
        sleep 2
        $0 start
        echo "===restartAll success==="
        ;; 
	
check)
	## 检查timeservice
	P_ID=`ps -ef | grep -w ${timeservice##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===timeservice process not alive"
        else
            echo "===timeservice process alive"
        fi

	## 检查manager
	P_ID=`ps -ef | grep -w ${manager##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===manager process not alive"
        else
            echo "===manager process alive"
        fi

	## 检查ftpservice
	P_ID=`ps -ef | grep -w ${ftpservice##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ftpservice process not alive"
        else
            echo "===ftpservice process alive"
        fi

	## 检查ftpschedule
	P_ID=`ps -ef | grep -w ${ftpschedule##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ftpschedule process not alive"
        else
            echo "===ftpschedule process alive"
        fi

	## 检查apiservice
	P_ID=`ps -ef | grep -w ${apiservice##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===apiservice process not alive"
        else
            echo "===apiservice process alive"
        fi

	## 检查db2file
	P_ID=`ps -ef | grep -w ${db2file##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===db2file process not alive"
        else
            echo "===db2file process alive"
        fi


	## 检查ftpschedule
	P_ID=`ps -ef | grep -w ${file2db##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===file2db process not alive"
        else
            echo "===file2db process alive"
        fi

	## 检查ftpschedule
	P_ID=`ps -ef | grep -w ${file2hdfs##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===file2hdfs process not alive"
        else
            echo "===file2hdfs process alive"
        fi

	## 检查ftpschedule
	P_ID=`ps -ef | grep -w ${file2kafka##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===file2kafka process not alive"
        else
            echo "===file2kafka process alive"
        fi

	## 检查ftpschedule
	P_ID=`ps -ef | grep -w ${fpf##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===fpf process not alive"
        else
            echo "===fpf process alive"
        fi

	## 检查ftpschedule
	P_ID=`ps -ef | grep -w ${ftpdl##*/} | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ftpdl process not alive"
        else
            echo "===ftpdl process alive"
        fi
 
        echo "===check finished==="
        ;;   
esac	
exit 0

 

<think>好的,我现在需要帮用户解决如何编写一个自动重启Java应用程序(基于JAR包)的Shell脚本的问题。首先,我得回顾用户提供的引用内容,看看有没有可用的信息。 用户提到了三个引用,其中引用[2]和[3]都涉及到批量启动、停止和重启Jar包的脚本。特别是引用[3]中的脚本有备份应用文件、检查应用是否运行、启动应用并写入PID文件,以及优雅关闭的功能。这些信息应该对编写重启脚本有帮助。 接下来,我需要考虑用户的需求:自动重启Jar包的Shell脚本。这意味着脚本需要能够停止正在运行的应用,然后再启动它。可能需要先检查进程是否存在,如果存在就杀死进程,然后重新启动。 根据引用[2]中的示例,停止应用时使用了`ps -ef`查找进程ID,然后通过`kill`命令终止进程。而启动应用则是使用`nohup java -jar`命令,并将进程ID保存到文件中。引用[3]还提到了备份和优雅关闭,但用户可能主要关注重启功能,所以可能需要简化步骤。 接下来,我需要组合这些步骤到一个脚本中。大致流程应该是: 1. 定义Jar包的路径和名称。 2. 停止正在运行的Jar应用:查找进程ID并杀死。 3. 等待一段时间确保进程已经停止。 4. 启动新的Jar应用,并记录新的PID。 5. 添加执行权限和调试信息,如用户示例中的`chmod +x`和`set`命令。 需要注意的是,停止应用时可能会有多个进程匹配,需要确保准确获取正确的PID。另外,启动时使用`nohup`和后台运行,避免脚本挂起。 可能还需要考虑日志输出,将启动日志保存到文件,方便后续查看。此外,添加错误处理,比如在停止失败时提示,或者启动失败时退出脚本。 现在,将这些步骤转化为具体的Shell命令。停止部分可以参考引用[2]中的`P_ID`获取方式,使用`grep`和`awk`提取PID启动部分使用`nohup`,并重定向输出到日志文件。同时,保存PID到文件,以便后续管理。 最后,测试脚本的正确性,包括权限设置(chmod +x),运行脚本,并检查进程是否正常重启。可能还需要调试模式,使用`set -x`来显示执行过程,方便排查问题。 总结下来,脚本的结构大致如下: - 变量定义 - 停止函数 - 启动函数 - 重启函数(先停止,再启动- 主逻辑调用重启函数 - 添加执行权限和调试选项 需要确保每一步的错误处理,比如如果停止失败是否继续启动,或者是否需要强制终止进程。用户可能需要一个简单可靠的脚本,所以保持逻辑清晰,避免复杂判断,除非用户有特别需求。 现在,将这些思考整理成一个脚本示例,并附上相关说明,帮助用户理解每个步骤的作用。同时,生成相关问题,引导用户进一步学习优化和扩展脚本的功能。</think>### 自动重启Java应用的Shell脚本编写指南 以下是实现JAR包程序自动重启Shell脚本示例及分步说明,结合了进程管理、优雅关闭和日志记录功能: ```bash #!/bin/bash set -e # 遇到错误立即退出 # ========== 配置区 ========== APP_NAME="myapp.jar" # 应用程序JAR包名称 APP_PATH="/opt/app" # 应用程序安装目录 LOG_FILE="app_restart.log" # 日志文件路径 MAX_WAIT=10 # 等待进程停止的最大秒数 # ========== 函数定义 ========== stop_application() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] 正在停止应用..." PID=$(ps -ef | grep java | grep "$APP_NAME" | grep -v grep | awk '{print $2}') if [ -n "$PID" ]; then kill -15 "$PID" # 先尝试优雅关闭 sleep 3 # 检查进程是否仍然存在 if ps -p "$PID" > /dev/null; then echo "优雅关闭失败,强制终止进程 $PID" kill -9 "$PID" fi else echo "应用未在运行" fi } start_application() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] 正在启动应用..." nohup java -jar "$APP_PATH/$APP_NAME" >> "$APP_PATH/$LOG_FILE" 2>&1 & NEW_PID=$! echo "启动成功,进程ID: $NEW_PID" } # ========== 主逻辑 ========== cd "$APP_PATH" || exit 1 stop_application start_application echo "应用重启完成" ``` ### 关键功能说明: 1. **进程识别** 使用`ps -ef | grep java`组合命令精准定位目标进程[^2],通过`awk '{print $2}'`提取进程ID 2. **优雅关闭机制** - 首先发送SIGTERM(`kill -15`)信号允许程序完成资源清理 - 3秒后检查进程状态,未关闭则发送SIGKILL(`kill -9`)[^3] 3. **日志管理** 使用`>>`追加模式记录日志,避免覆盖历史记录 4. **错误防护** - `set -e`确保脚本在出错时立即终止 - `cd "$APP_PATH" || exit 1`防止目录切换失败 ### 使用步骤: 1. 保存为`restart.sh` 2. 授权执行权限: ```bash chmod +x restart.sh ``` 3. 执行脚本: ```bash ./restart.sh ``` ### 扩展建议: - 添加邮件通知功能,在重启失败时发送告警 - 集成健康检查,确保应用真正启动成功 - 设置最大重试次数,防止无限重启循环
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早退的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值