jar包开机自启动脚本

发布时间:2023年12月17日
#!/bin/sh
#配置哪些包需要启动哪些包不需要启动
export egsAdmin_enable=true
export iparkAccesscontrol_enable=true
export egsHikplat_enable=true

#jar包路径
export egsAdmin=.../egs-admin-1.0.0-SNAPSHOT.jar
export iparkAccesscontrol=.../ipark-accesscontrol-1.0.0-SNAPSHOT.jar
export egsHikplat=.../egs-hikplat-1.0.0-SNAPSHOT.jar

#jar包配置文件路径
export egsAdmin_config=.../config/

#各个服务端口
export egsAdmin_port=端口号
export iparkAccesscontrol_port=端口号
export egsHikplat_port=端口号

case "$1" in
 
start)
	if($egsAdmin_enable);then
		## 启动egsAdmin
		echo "--------egsAdmin 开始启动--------------"
		nohup java -jar r -Dspring.config.location=$egsAdmin_config $egsAdmin >/dev/null 2>&1 &
		egsAdmin_pid=`lsof -i:$egsAdmin_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$egsAdmin_pid" ]
		    do
		      egsAdmin_pid=`lsof -i:$egsAdmin_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "egsAdmin pid is $egsAdmin_pid" 
		echo "--------egsAdmin 启动成功--------------"
	fi
 
	if($iparkAccesscontrol_enable);then
		## 启动iparkAccesscontrol
		echo "--------iparkAccesscontrol 开始启动--------------"
		nohup java -jar $iparkAccesscontrol >/dev/null 2>&1 &
		iparkAccesscontrol_pid=`lsof -i:$iparkAccesscontrol_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$iparkAccesscontrol_pid" ]
		    do
		      iparkAccesscontrol_pid=`lsof -i:$iparkAccesscontrol_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "iparkAccesscontrol pid is $iparkAccesscontrol_pid" 
		echo "--------iparkAccesscontrol 启动成功--------------"
	fi
 
	if($egsHikplat_enable);then
		## 启动egsHikplat
		echo "--------egsHikplat 开始启动--------------"
		nohup java -jar $egsHikplat >/dev/null 2>&1 &
		egsHikplat_pid=`lsof -i:$egsHikplat_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$egsHikplat_pid" ]
		    do
		      egsHikplat_pid=`lsof -i:$egsHikplat_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "egsHikplat pid is $egsHikplat_pid" 
		echo "--------egsHikplat 启动成功--------------"
	fi

        echo "===startAll success==="
        ;;

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

        echo "===check finished==="
        ;;   
esac	
exit 0

最后加入开机重启
crontab -e
@reboot /jar_start.sh start(改成自己的脚本路径)

文章来源:https://blog.csdn.net/qq_41178109/article/details/135029762
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。