shell脚本启动多jar包

shell脚本启动jar包

一、脚本

  1. 通过 true或false控制是否启动
#!/bin/sh
export common_service_enable=true
export business_service_enable=true
export xiaopandian_web_enable=true
 
export common_service=/mnt/xiaopandian-api/dev-common-service.jar
export business_service=/mnt/xiaopandian-api/dev-business-service.jar
export xiaopandian_web=/mnt/xiaopandian-api/dev-xiaopandian-web.jar

export common_service_port=17001
export business_service_port=17002
export xiaopandian_web_port=17003


 
case "$1" in
 
start)
	if($common_service_enable);then
		## 启动timeservice
		echo "--------common_service 开始启动--------------"
		nohup java -jar $common_service >/dev/null 2>&1 &
		common_service_pid=`lsof -i:$common_service_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$common_service_pid" ]
		    do
		      common_service_pid=`lsof -i:$common_service_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "common_service pid is $common_service_pid" 
		echo "--------common_service 启动成功--------------"
	fi
 
	if($business_service_enable);then
		## 启动business_service
		echo "--------business_service 开始启动--------------"
		nohup java -jar $business_service >/dev/null 2>&1 &
		business_service_pid=`lsof -i:$business_service_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$business_service_pid" ]
		    do
		      business_service_pid=`lsof -i:$business_service_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "business_service pid is $business_service_pid" 
		echo "--------business_service 启动成功--------------"
	fi
 
	if($xiaopandian_web_enable);then
		## 启动ftpschedule
		echo "--------xiaopandian_web 开始启动--------------"
		nohup java -jar $xiaopandian_web >/dev/null 2>&1 &
		xiaopandian_web_pid=`lsof -i:$xiaopandian_web_port|grep "LISTEN"|awk '{print $2}'`
		until [ -n "$xiaopandian_web_pid" ]
		    do
		      xiaopandian_web_pid=`lsof -i:$xiaopandian_web_port|grep "LISTEN"|awk '{print $2}'`  
		    done
		echo "xiaopandian_web pid is $xiaopandian_web_pid" 
		echo "--------xiaopandian_web 启动成功--------------"
	fi

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

二、过程

  1. 将.sh脚本拖到服务器

  2. 将.sh文件赋予权限

    chmod 775 XXX.sh
    
  3. 执行

    # 启动
    ./XXX.sh start 
    # 停止
    ./XXX.sh stop
    # 检查
    ./XXX.sh check
    # 重启
    ./XXX.sh restart
    

三、问题

  1. Linux执行.sh文件,提示No such file or directory的问题

    • 原因:在windows中写好shell脚本测试正常,但是上传到 Linux 上以脚本方式运行命令时提示No such file or directory错误,那么一般是文件格式是dos格式的缘故,改成unix 格式即可。一般有如下几种修改办法。

    • 解决办法:

      用vim打开该sh文件,输入:
      :set ff 
      回车,显示fileformat=dos,重新设置下文件格式:
      :set ff=unix 
      保存退出: 
      :wq 
      再执行,竟然可以了 
      
  2. 解决Ubuntu下编译.sh文件报错 “[: XXXX: unexpected operator”

    • 原因:因为Ubuntu默认的sh是连接到dash的,dash跟bash的不兼容所以出错了。

    • 解决办法:命令行输入后,选择NO

      sudo dpkg-reconfigure dash
      
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值