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

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

 我这里有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

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

早退的程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值