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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


名称栏目:shell脚本启动多jar包-创新互联
文章源于:http://csdahua.cn/article/epesd.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流