linux下进行服务管理的shell脚本

发布时间:2020-07-16编辑:脚本学堂
分享一个linux下管理服务的shell脚本,有需要的朋友参考下吧。

本shell脚本,可以做到:
通过设置初始化参数首次用于启动各种服务,如Informatica的服务器/ Websphere服务器的可重复使用的脚本。

代码:
 

复制代码 代码示例:

#!/bin/ksh
#------------------------------------------------------------------------------
# Program Name       :start.sh
# Description        :This program is for Menu to provide user various option to Start Various services & Server  #
# Parameters         :None

clear;
. $HOME/prog/RUN/shell/StartMgr/.exportParams
export LOG_PATH=$HOME/prog/RUN/shell/StartMgr
export LOGFNAME=StartMgr.log;

tput bold;
{
  PS3="Please enter your choice (1-9):"
  select  myselection in "Stop TED Server" "Start Informatica Server"  "Stop Informatica Server"  "Start Informatica Repository Server"   "Start WebSphere Server" "Stop WebSphere Server" "Clear the screen" "Quit" ;
  do
 case $myselection in
        # Code to Stop TED Server 
        "Stop TED Server")
         ping ted >> $LOG_PATH/$LOGFNAME 2>&1
          if [ $? -eq 0 ]; then
            printf "The TED server is already running..Would you like Stop the server y/nn";
            read $input
            #  if [[ $input -eq "y" ]]
            echo " TED SERVER IS GETTING STOPPED.........within 10Min."
            (
            echo  "SYSTEM SHUTDOWN IN 10 MINUTES!" | wall
            sleep 300; echo "SYSTEM SHUTDOWN IN 5 MINUTES!" | wall
            sleep 240; echo "SYSTEM SHUTDOWN IN 1 MINUTE!" | wall
            #sleep 60; shutdown
            )&
            #fi;
          else
               echo "STATUS CODE IS " $?    
               ping ted >> $LOG_PATH/$LOGFNAME 2>&1
          fi;
     ;;
    # Code to start Informatica Server starts here
    "Start Informatica Server")
          echo "Informatica Server is getting Started...."
           # Start Code to start Infomatica Server
           WORKING_DIR=`pwd`
           # The following ps returns 1 if the pmserver is UP and 0 shen it is down
           #RET_CODE=`ps -aef |grep pmserver|awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
           cd $INF_HOME
           pmcmd pingserver -s $INF_SERVER:$INF_PORT >> $LOG_PATH/$LOGFNAME 2>&1
            if [ $? -eq 0 ]; then
                    printf "The informatica server is already running..Stop server and startn";
            else
                    pmserver $INF_CONFIG_FILENAME >> $LOG_PATH/$LOGFNAME 2>&1
                    #RET_CODE=`ps -aef |grep pmserver | awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
                    echo "It may take around 60 seconds to Initialize the server.Plz Wait"
                    sleep 60
                    pmcmd pingserver -s $INF_SERVER:$INF_PORT >> $LOG_PATH/$LOGFNAME 2>&1
                    if [ $? -eq 0 ] ; then
                      print "PMsever started successfullyn "
                    else
                      printf "There is some problem in starting PMserver, Contact Administratorn See The Log in the location /ipc7/pc/TEDPROD4x.log"
                    fi;
            fi;
            cd $WORKING_DIR;
 
    # End Code
    ;;
 
    #Code to stop the Informatica Server
    "Stop Informatica Server")
        WORKING_DIR=`pwd`
        cd $INF_HOME
      echo " INFORMATICA SERVER IS GETTING STOPPED.........Plz Wait..."
        #RET_CODE=`ps -aef |grep pmserver|awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
       pmcmd pingserver -s $INF_SERVER:$INF_PORT >> $LOG_PATH/$LOGFNAME 2>&1  
      if [ $? -eq 0 ]; then
          (
              # Run Background Alerts to all users
               echo  "INFORMATICA SHUTDOWN IN 1 MINUTES!" | wall
               sleep 30; echo "INFORMATICA SHUTDOWN IN 30 SECONDS!" | wall
             )&
             #To run in Background
            echo "The Following command has been executed:"
        echo "pmcmd shutdownserver -s $INF_SERVER:$INF_PORT -u $INF_USER -p $INF_PASSWD -stop"
        pmcmd shutdownserver -s $INF_SERVER:$INF_PORT -u $INF_USER -p $INF_PASSWD -stop >> $LOG_PATH/$LOGFNAME 2>&1
        if [ $? -eq 0 ] ; then
         printf "PMserver stopped successfullyn";
        else
         printf "There is some problem in stopping PMserver, Contact Administratorn";
        fi;
        else
        printf "PMserver already stoppedn" 
      fi;
      cd $WORKING_DIR;
        ;;
  #End code

    # Code to start Informatica Repository Server starts here
    "Start Informatica Repository Server")
         echo "Informatica Repository Server is getting Started...."
         # Start Code to start Infomatica Repository Server
         WORKING_DIR=`pwd`
         # The following ps returns 1 if the pmrepserver is UP and 0 then it is down
         cd $INFREP_HOME
         RET_CODE=`ps -aef |grep pmrepserver|grep ted4x| grep -v grep |awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
         if [ $? -eq 0 ]; then
                printf "The Informatica Repository server is already running..Stop server and startn";
         else
                pmrepserver $INFREP_CONFIG >> $INFREP_LOGPATH/$INFREP_LOGFNAME 2>&1
                # Start Agent also
                #pmrepagent >> $INFREP_LOGPATH/$INFREP_LOGFNAME 2>&1
                # pmserver $INF_HOME/$INF_CONFIG_FILENAME >> $LOG_PATH/$LOGFNAME 2>&1
                #RET_CODE=`ps -aef |grep pmrepagent | awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
                echo "It may take around 60 seconds to Initialize the server.Plz Wait"
                #pmcmd pingserver -s $INF_SERVER:$INF_PORT >> $LOG_PATH/$LOGFNAME 2>&1
                #Need to Parametrize Username
                sleep 60;
                RET_CODE=`ps -aef |grep pmrepserver|grep ted4x| grep -v grep |awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
                if [ $? -eq 0 ] ; then
                  print "PMsever started successfullyn "
                else
                  printf "There is some problem in starting PMserver, Contact Administratorn See The Log in the location /ipc7/pc/TEDPROD4x.log"
                fi;
            fi;
            cd $WORKING_DIR;

    # End Code
    ;;
    # Code to start WebSphere Server starts here
    "Start WebSphere Server")
           echo "WebSphere Server is getting Started...."
            # Start Code to strt WebSphere Server
            WORKING_DIR=`pwd`
            cd $WEBS_HOME
            # The following ps returns 1 if the pmserver is UP and 0 shen it is down
            #RET_CODE=`ps -aef |grep $WEBS_SERVERNAME |awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
            serverStatus.sh $WEBS_SERVERNAME -logfile $WEBS_LOGPATH/$WEBS_LOGFNAME | grep STARTED
             if [ $? -eq 0 ]; then
                      printf "The WEBSPHERE server is already running..Stop server and startn";
                      #RET_CODE=`ps -aef |grep pmserver | awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
            
             else
                      echo "It may take around 60 seconds to Initialize the server.Plz Wait"
                      startServer.sh $WEBS_SERVERNAME -username $WEBS_USER -password $WEBS_PASSWD -logfile $WEBS_LOGPATH/$WEBS_LOGFNAME ;
                      if [ $? -eq 0 ] ; then
                        print "WEBSPHERE started successfullyn "
                      else
                        printf "There is some problem in starting WEBSPHERE, Contact Administratorn See The Log in the location " $WEBS_LOGPATH/$WEBS_LOGFNAME
                      fi;
              fi;
              cd $WORKING_DIR;
      # End Code
        ;;
    # Code to stop WebSphere Server starts here
    "Stop WebSphere Server")
        echo "WebSphere Server is getting Stop...."
        # Start Code to stop WebSphere Server
        WORKING_DIR=`pwd`
        cd $WEBS_HOME
        # The following ps returns 1 if the pmserver is UP and 0 then it is down
        #RET_CODE=`ps -aef |grep $WEBS_SERVERNAME |awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
        serverStatus.sh $WEBS_SERVERNAME -logfile $WEBS_LOGPATH/$WEBS_LOGFNAME | grep stopped
         if [ $? -eq 0 ]; then
                printf "The WEBSPHERE server is already stopped..n";
                #RET_CODE=`ps -aef |grep pmserver | awk '{if ($3 == 1)  print 0; }'|grep -c 0`;
         else       
             echo "It may take around 60 seconds to Stop the server.Plz Wait"
             (
             echo  "WEBSPHERE SHUTDOWN IN 1 MINUTES!" | wall
             sleep 30; echo "WEBSPHERE SHUTDOWN IN 30 SECONDS!" | wall
             sleep 30;
             )&
             stopServer.sh $WEBS_SERVERNAME -username $WEBS_USER -password $WEBS_PASSWD -logfile $WEBS_LOGPATH/$WEBS_LOGFNAME -trace ;
              if [ $? -eq 0 ] ; then
                print "WEBSPHERE Stopped successfullyn "
              else
                printf "There is some problem in stopping WEBSPHERE, Contact Administratorn See The Log in the location " $WEBS_LOGPATH/$WEBS_LOGFNAME
              fi;
      fi;
      cd $WORKING_DIR;
# End Code
      ;;
    "Clear the screen") clear;;
 
    "Quit") break ;;
    
    *) echo "Invalid option"
    
     ;;
  esac;                                                         
 done     
}
 

注意:本脚本用ksh编写,其它类型的shell,请自行修改相关代码。