Linux 自啟動執行指令碼
阿新 • • 發佈:2019-01-05
#!/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