chk启动脚本

#!/bin/bash 
# chkconfig : 35 99 05 
# description: Test chkconfig 
# file: /etc/init.d/foo 
env > /tmp/ENVLOG 
35 : 就是哪种runlevel下启动 
99:在rc3.d和rc5.d下产生S99foo (越小优先权越高)
05: 在rc0.d,rc1.d,rc2.d,rc4.d,rc6.d下产生K05f00 (越小优先权越高)
S:代表Start 
K:代表Kill 

1 tomcat
#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcat
# Tomcat auto-start
# Source function library.
. /etc/init.d/functions
# source networking configuration.
. /etc/sysconfig/network
RETVAL=0
export JAVA_HOME=/usr/java/jdk1.7.0_80
export PATH=$JAVA_HOME/bin:$PATH
export CATALINA_HOME=/opt/tomcat-v21

tomcat_pid() {
        echo `ps -ef | grep $CATALINA_HOME | grep -v grep | grep jdk | tr -s " "|cut -d" " -f2`
}

start()
{
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            echo $"Starting Tomcat"
                $CATALINA_HOME/bin/startup.sh
            RETVAL=$?
            echo " OK"
            return $RETVAL
        fi
}

status() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ];then
    echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi
}

stop()
{
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
                $CATALINA_HOME/bin/shutdown.sh
            RETVAL=$?
            sleep 1
            ps -ef | grep tomcat | grep -v grep | grep jdk | awk '{print $2}'| xargs kill -9
            echo " OK"
            # [ $RETVAL -eq 0 ] && rm -f /var/lock/...
            return $RETVAL
        fi
}

case "$1" in
 start) 
        start
        ;;
 stop)  
        stop
        ;;
                                                
 restart)
         echo $"Restaring Tomcat"
         $0 stop
         sleep 1
         $0 start
         ;;
 status) 
        status
        ;;

      *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac
exit $RETVAL
----------------------------------------------------------------------------------------------------------------
2 tomcat
#!/bin/bash
#
# chkconfig: 2345 95 15 
# description: Tomcat start/stop/status script
. /etc/init.d/functions
# source networking configuration.
. /etc/sysconfig/network
#Location of JAVA_HOME (bin files)
export JAVA_HOME=/usr/java/jdk1.7.0_80

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the configuration files of this instance of Tomcat
CATALINA_HOME=/opt/tomcat-v21

#TOMCAT_USER is the default user of tomcat
TOMCAT_USER=root

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -ef | grep $CATALINA_HOME | grep -v grep | grep jdk | tr -s " "|cut -d" " -f2`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ];then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    echo -e "\e[00;32mStarting tomcat\e[00m"
    if [ `user_exists $TOMCAT_USER` = "1" ];then
      su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
    else
      $CATALINA_HOME/bin/startup.sh
    fi
    status
  fi
  return 0
}

status(){
  pid=$(tomcat_pid)
  if [ -n "$pid" ];then
    echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ];then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
        $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ];then
      echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      kill -9 $pid
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

user_exists(){
  if id -u $1 >/dev/null 2>&1; then
    echo "1"
  else
    echo "0"
  fi
}

case $1 in
        start)
          start
        ;;

        stop)  
          stop
        ;;

        restart)
          stop
          start
        ;;

        status)
	  status
        ;;

        *)
	  echo -e $TOMCAT_USAGE
        ;;
esac    
exit 0

你可能感兴趣的:(chk启动脚本)