2. Tomcat常用脚本

最后更新于:2022-04-02 07:38:41

### JDK一键部署脚本 ```shell #!/bin/sh #Author:BrandynX #Date:2017-05-3 #Remark: JDK automatically deploys the script #Receive the variable jdk_version="" while getopts "v:" arg do case ${arg} in v) jdk_version=${OPTARG} ;; \?) echo "Usage: args [-v]" echo " -v 1.7.80 , Please enter the version of the deployment." echo " sh auto_deploy_jdk.sh -v 1.7.80 #1.7.80 is equivalent to jdk 7u80" exit 1 ;; esac done #Public Function: Judgment UID function_check_uid(){ if [ $UID -eq 0 ];then return 0 else echo "Please use root run script." exit 1 fi } # Custom Function: Judgment tomcat_version function_check_parameter(){ if [ -z ${jdk_version} ] ;then echo "Usage: args [-v]" echo " -v 1.7.80 , Please enter the version of the deployment." echo " sh auto_deploy_jdk.sh -v 1.7.80 #1.7.80 is equivalent to jdk 7u80" exit fi } function_check_uid function_check_parameter #unzip and copy to the deployment directory [ -d /opt/jdk-${jdk_version} ] && rm -rf /opt/jdk-${jdk_version} if [ -f /opt/jdk-${jdk_version}.zip ];then unzip /opt/jdk-${jdk_version}.zip -d /opt/ else echo "ERROR: JDK zipfile not exist" fi #Mobile directory rm -rf /app/jdk-${jdk_version} rm -f /app/jdk mv /opt/jdk-${jdk_version} /app && chmod 755 -R /app/jdk-${jdk_version} if [ $? -ne 0 ];then exit fi ln -s /app/jdk-${jdk_version} /app/jdk #Generate variable files and load them rm -f /etc/profile.d/env_java.sh echo 'export JAVA_HOME=/app/jdk' >> /tmp/env_java.sh echo 'export JRE_HOME=${JAVA_HOME}/jre' >> /tmp/env_java.sh echo 'export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib'>> /tmp/env_java.sh echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /tmp/env_java.sh mv /tmp/env_java.sh /etc/profile.d/ && chmod 755 /etc/profile.d/env_java.sh source /etc/profile.d/env_java.sh ``` #### Tomcat一键部署脚本 ```shell #!/bin/sh #Author:BrandynX #Date:2017-05-3 #Remark: Tomcat automatically deploys the script #Set the variable script_logdir="/app/logs" shell_name=$(basename $0) shell_lockfile="/tmp/${shell_name}.lock" shell_logfile="$script_logdir/${shell_name}.log" tomcat_version=$1 tomcat_start_script="multiple_tomcat.sh" #Public Function: Judgment lockfile function_create_lockfile(){ if [ -f $shell_lockfile ];then function_writelog_simple "[Warning] The script is running! please wait." exit else echo $$ > $shell_lockfile fi } #Public Function: Directly_delete shell lockfile function_delete_lockfile(){ rm -f ${shell_lockfile} && exit 1 } #Public Function: Judgment after delete shell lockfile function_judgment_delete_lockfile(){ if [ $? -ne 0 ];then function_delete_lockfile exit fi } #Public Function: Judgment UID function_check_uid(){ if [ $UID -eq 0 ];then return 0 else echo "Please use root run script." exit 1 fi } #Public Function: Judgment command return value function_judgment_command(){ [ $? -eq 0 ] && return 0 || exit } #Public Function: Judgment and write Log function_writelog_judgment(){ if [ $? -eq 0 ];then log_info=$1 echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") [Note]: ${shell_name} : ${log_info} executed sucessfully" >> ${shell_logfile} else echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") [Error]: ${shell_name} : ${log_info} executed failed" |tee -a ${shell_logfile} exit fi } #Public Function: Simple write Log function_writelog_simple(){ log_info=$1 echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") : ${shell_name} : ${log_info} " >> ${shell_logfile} } # Custom Function: Judgment tomcat_version 是否有值 function_check_parameter(){ if [ -z ${tomcat_version} ] ;then echo "Please input Tomcat version" exit fi } #Custom Function: Unzip and copy to the deployment directory function_unzip_tomcat(){ [ -d /opt/tomcat-${tomcat_version} ] && rm -rf /opt/tomcat-${tomcat_version} if [ -f /opt/tomcat-${tomcat_version}.zip ];then unzip /opt/tomcat-${tomcat_version}.zip -d /opt/ if [ $? -eq 0 ];then function_writelog_simple " [Note] Unzip tomcat sucessfully." else function_writelog_simple " [Error] Unzip tomcat failed." function_delete_lockfile fi else function_writelog_simple " [ERROR] Tomcat zipfile not exist" function_delete_lockfile fi } #Custom Function: Mobile directory function_deploy_tomcat(){ rm -rf /app/tomcat-${tomcat_version} /app/tomcat mv /opt/tomcat-${tomcat_version} /app && chmod 755 -R /app/tomcat-${tomcat_version} && chown root.root -R /app/tomcat-${tomcat_version} function_judgment_delete_lockfile ln -s /app/tomcat-${tomcat_version} /app/tomcat function_writelog_simple "[Note] Deploy tomcat sucessfully" } # Custom Function: Copy multiple instance startup scripts function_copy_script(){ if [ -f /opt/${tomcat_start_script} ];then cp /opt/${tomcat_start_script} /app/ chmod 755 /app/${tomcat_start_script} function_writelog_simple "[Note] Deploy tomcat script sucessfully" else echo "ERROR: Tomcat startup script not exist" function_delete_lockfile fi } tomcat_install_main(){ function_unzip_tomcat function_deploy_tomcat function_copy_script } function_check_uid function_check_parameter function_create_lockfile tomcat_install_main function_delete_lockfile ``` #### Tomcat服务启停脚本 ```shell #!/bin/sh # description: 启动tomcat多实例. #Load public functions . /etc/init.d/functions #Initialize the variable RETVAL=$? tomcat_version="" oprating="" #Call getopts while getopts "n:o:" arg do case $arg in n) tomcat_version=$OPTARG ;; o) operating=$OPTARG ;; ?) echo "Usage: multiple_tomcat_startup.sh -n tomcat1 -o {start|stop}" echo " -n tomcat1 , Please enter the name after the deployment is complete." echo " -o {start|stop} , Please enter operation content." exit 1 ;; esac done # Tomcat dir export CATALINA_BASE="/app/$tomcat_version/" export CATALINA_HOME="/app/$tomcat_version" case "$operating" in start) if [ $(ps -ef | grep java| grep tomcat1| grep -v grep |wc -l) -gt 0 ];then echo "[Error] $tomcat_version is has started." exit fi if [ -f $CATALINA_HOME/bin/startup.sh ];then echo $"Start $tomcat_version" $CATALINA_HOME/bin/startup.sh echo "Trace log command: tail -100f /app/${tomcat_version}/logs/catalina.out" fi ;; stop) if [ -f $CATALINA_HOME/bin/shutdown.sh ];then echo $"Stop $tomcat_version" $CATALINA_HOME/bin/shutdown.sh sleep 3 [ $(ps -ef | grep java| grep tomcat1| grep -v grep |wc -l) -eq 0 ] && echo "[Node] $tomcat_version is stops successfully." || echo "[Warning] $tomcat_version has not stopped yet,please wait." fi ;; *) echo "Usage: multiple_tomcat_startup.sh -n tomcat1 -o {start|stop}" echo " -n tomcat1 , Please enter the name after the deployment is complete." echo " -o {start|stop} , Please enter operation content." exit 1 ;; esac exit $RETVAL ```
';