Linux 自启动执行脚本

#!/bin/bash
#chkconfig: 2340 20 80
#description:sgapp manage util
#processname:sgapp
APP_HOME=/opt/sgUtils/sgzt
case $1 in
	start) $APP_HOME/runAll.sh start;;
	stop) $APP_HOME/runAll.sh stop;;
	status) $APP_HOME/runAll.sh status;;
	restart) $APP_HOME/runAll.sh restart;;
	*) echo "require start|stop|status|restart" ;;
esac

存放目录 /etc/rc.d/init.d

启动脚本:

#!/bin/bash

# 当前目录
curDir=`pwd`

# app 目录
appDir='/opt/app'

# tomcat 应用列表
tomcatApps=(tomcat-crm tomcat-wechat-h5 tomcat-opms)
# spring 应用列表
springApps=(crm-server crm-timed-task flow-server process-server mail-server opms-server wechat-tokenServer wechat-gateWay-inner wechat-businessServer msg-distribute)

# 启动模式
action=$1

app_status() {
	if [ ! -d "${appDir}/$1" ]; then
		return 2
	else
		status=`ps -ef | grep $1 | grep -v grep`
		if [ -n "$status" ]; then
			return 0
		else
			return 1
		fi
	fi
}

spring_app_uninstall() {
	spring_app_stop $1
	if [ -d "${appDir}/$1" ]; then
		cd ${appDir}
		rm -rf $1
		echo "应用[$1]卸载成功"
	else
		echo "应用[$1]无需卸载"
	fi
}

spring_app_install() {
	if [ ! -f "${appDir}/$1.tar.gz" ]; then
		echo "安装包[$1.tar.gz]不存在,已忽略"
		return
	fi
	echo "1. 卸载旧的应用[$1]"
	spring_app_uninstall $1
	echo "2. 安装应用[$1]"
	cd ${appDir}
	tar -xf $1.tar.gz
	rm -rf $1.tar.gz
	echo "3. 应用[$1]安装完成"
	
}

tomcat_app_uninstall() {
	tomcat_app_stop $1
	if [ -d "${appDir}/$1/webapps" ]; then
		cd ${appDir}/$1/webapps
		if [ -f $1 ]; then
			rm -rf $1
		fi
		if [ -f "$1.war" ]; then
			rm -rf $1.war
		fi
		echo "应用[$1]卸载成功"
	else
		echo "应用[$1]无需卸载"
	fi
}

tomcat_app_install() {
	if [ ! -f "${appDir}/$1.war" ]; then
		echo "安装包[$1.war]不存在,已忽略"
		return
	fi
	echo "1. 卸载旧的应用[$1]"
	tomcat_app_uninstall $1
	echo "2. 安装应用[$1]"
	cd ${appDir}
	cp $1.war $1/webapps
	rm -rf $1.war
	echo "3. 应用[$1]安装完成"
}

spring_app_start() {
	app_status $1
	ret=$?
	if [ $ret -eq 1 ]; then
		echo "启动应用[$1]"
		cd ${appDir}/$1/bin
		./run.sh start
	elif [ $ret -eq 0 ]; then
		echo "应用[$1]已启动!"
	elif [ $ret -eq 2 ]; then
		echo "应用[$1]不存在"
	fi
	
}

spring_app_stop() {
	app_status $1
	ret=$?
	if [ $ret -eq 0 ]; then
		echo "停止应用[$1]"
		cd ${appDir}/$1/bin
		./run.sh stop
		echo "应用[$1]已停止!"
	elif [ $ret -eq 1 ]; then
		echo "应用[$1]已停止!"
	elif [ $ret -eq 2 ]; then
		echo "应用[$1]不存在"
	fi
}

tomcat_app_start() {
	app_status $1
	ret=$?
	if [ $ret -eq 1 ]; then
		echo "启动应用[$1]"
		cd ${appDir}/$1/bin
		./startup.sh
	elif [ $ret -eq 0 ]; then
		echo "应用[$1]已启动!"
	elif [ $ret -eq 2 ]; then
		echo "应用[$1]不存在"
	fi
}

tomcat_app_stop() {
	app_status $1
	ret=$?
	if [ $ret -eq 0 ]; then
		echo "停止应用[$1]..."
		cd ${appDir}/$1/bin
		./shutdown.sh
	elif [ $ret -eq 1 ]; then
		echo "应用[$1]已停止!"
	elif [ $ret -eq 2 ]; then
		echo "应用[$1]不存在"
	fi
}

run_install() {
	for app in ${springApps[*]}
	do
		spring_app_install $app
	done
	
	for app in ${tomcatApps[*]}
	do
		tomcat_app_install $app
	done
	cd ${curDir}
}

run_uninstall() {
	for app in ${springApps[*]}
	do
		spring_app_uninstall $app
	done
	
	for app in ${tomcatApps[*]}
	do
		tomcat_app_uninstall $app
	done
	cd ${curDir}
}

run_status() {
	for app in ${springApps[*]}
	do
		app_status $app
		ret=$?
		if [ $ret -eq 0 ]; then
			echo "应用[$app]已启动!"
		elif [ $ret -eq 1 ]; then
			echo "应用[$app]已停止!"
		elif [ $ret -eq 2 ]; then
			echo "应用[$app]不存在"
		fi
	done
	
	for app in ${tomcatApps[*]}
	do
		app_status $app
		ret=$?
		if [ $ret -eq 0 ]; then
			echo "应用[$app]已启动!"
		elif [ $ret -eq 1 ]; then
			echo "应用[$app]已停止!"
		elif [ $ret -eq 2 ]; then
			echo "应用[$app]不存在"
		fi
	done
	cd ${curDir}
}

# 停止所有spring app
run_stop() {
	for app in ${springApps[*]}
	do
		spring_app_stop $app
	done

	for app in ${tomcatApps[*]}
	do
		tomcat_app_stop $app
	done
	cd ${curDir}
}

# 启动所有spring app
run_start() {
	for app in ${springApps[*]}
	do
		spring_app_start $app
	done
	
	for app in ${tomcatApps[*]}
	do
		tomcat_app_start $app
	done
	cd ${curDir}
}

# 重启所有spring app
run_restart() {
	for app in ${springApps[*]}
	do
		spring_app_stop $app
		spring_app_start $app
	done
	
	for app in ${tomcatApps[*]}
	do
		tomcat_app_stop $app
		tomcat_app_start $app
	done
	cd ${curDir}
}

case "$action" in
    install|uninstall|status|start|stop|restart)
        run_$action
        ;;
    *)
        echo "Arguments error! [${action}]"
        echo "Usage: `basename $0` [install|uninstall|status|start|stop|restart]"
        ;;
esac


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值